在 pandas/matplotlib 中为图形标题拉取数据框名称

Posted

技术标签:

【中文标题】在 pandas/matplotlib 中为图形标题拉取数据框名称【英文标题】:Pulling Dataframe Name for graph title in pandas/matplotlib 【发布时间】:2022-01-18 20:16:51 【问题描述】:

我确信有一个简单的解决方案,但我有一个数据框列表,我使用相同的变量为其生成许多图表。

对于图表的标题,我想包含数据框名称,以便有助于组织。我试图调用 plt.title() 下的列表,但这没有奏效。如果您对此有解决方案,将不胜感激。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import sklearn

# import normal stats from csv in following order (Normal, Adversarial, Helpful, Neutral, Random)
# map 1
df1 = pd.read_csv('D:/Documents/Data Science/L4D2 Stats - No Mercy CSV files updated version/Normal/L4D2 Stats normal - c8m1 apartment.csv', header=0)
df2 = pd.read_csv('D:/Documents/Data Science/L4D2 Stats - No Mercy CSV files updated version/Adversarial/L4D2 Stats adversarial - c8m1 apartment.csv', header=0)
df3 = pd.read_csv('D:/Documents/Data Science/L4D2 Stats - No Mercy CSV files updated version/Helpful/L4D2 Stats helpful - c8m1 apartment.csv', header=0)
df4 = pd.read_csv('D:/Documents/Data Science/L4D2 Stats - No Mercy CSV files updated version/Neutral/L4D2 Stats neutral - c8m1 apartment.csv', header=0)
df5 = pd.read_csv('D:/Documents/Data Science/L4D2 Stats - No Mercy CSV files updated version/Random/L4D2 stats random - c8m1 apartment.csv', header=0)

#map 2

#df6 = pd.read_csv()
#df7 = pd.read.csv()
#df8 = pd.read.csv()
#df9 = pd.read.csv()
#df10 = pd.read.csv()


#map 3

#df11 = pd.read.csv()
#df12 = pd.read.csv()
#df13 = pd.read.csv()
#df14 = pd.read.csv()
#df15 = pd.read.csv()

#map 4

#df16 = pd.read.csv()
#df17 = pd.read.csv()
#df18 = pd.read.csv()
#df19 = pd.read.csv()
#df20 = pd.read.csv()

df_list = [df1, df2, df3, df4, df5]
# df_list2 = [df6, df7, df8, df9, df10]
# df_list3 = [df11, df12, df13, df14, df15]
# df_list4 = [df16, df17, df18, df19, df20]

for df in df_list:
    df = df
    df.describe()
    df.columns = ['Population', 'Special Spawns', 'Bot 1 Health', 'Bot 2 Health', 'Bot 3 Health', 'Bot 4 Health',
                  'Incaps', 'Bot 1 Intensity', 'Bot 2 Intensity', 'Bot 3 Intensity', 'Bot 4 Intensity']
    df['Special Spawns'] = df['Special Spawns'].fillna(0)
    df['Population'] = df['Population'].fillna(0)
    df['Bot 1 Health'] = df['Bot 1 Health'].fillna(0)
    df['Bot 2 Health'] = df['Bot 2 Health'].fillna(0)
    df['Bot 3 Health'] = df['Bot 3 Health'].fillna(0)
    df['Bot 4 Health'] = df['Bot 4 Health'].fillna(0)
    df['Incaps'] = df['Incaps'].fillna(0)
    df['Bot 1 Intensity'] = df['Bot 1 Intensity'].fillna(0)
    df['Bot 2 Intensity'] = df['Bot 2 Intensity'].fillna(0)
    df['Bot 3 Intensity'] = df['Bot 3 Intensity'].fillna(0)
    df['Bot 4 Intensity'] = df['Bot 4 Intensity'].fillna(0)

    normalised_df = (df-df.mean())/df.std()
    pd.options.display.float_format = ':,.8f'.format
    normalised_df.corr()
    plt.figure(figsize=(12, 8))
    sns.heatmap(normalised_df.corr(), annot=True, fmt=".4f")
    # input map name
    plt.title()
    plt.show()

【问题讨论】:

【参考方案1】:

您可以将数据框放入字典中并使用键作为绘图标题。

df_dict = 1: df1, 2: df2
for key, df in df_dict.items():
    plt.title(key)

尝试将对象的名称用作字符串并不是here 解释的好主意

【讨论】:

谢谢您,感谢您的指导,但我确实想知道我是否做错了。 我是在 for 循环中包含字典等,还是在之前定义字典? 你必须在for循环之前创建你的字典,否则你不能遍历它。 感谢您的帮助,我认为是这种情况,所以我这样做了。就目前而言,它只是将“5”作为每个图表的标题,我是否遗漏了什么? 抱歉忘记给你加标签

以上是关于在 pandas/matplotlib 中为图形标题拉取数据框名称的主要内容,如果未能解决你的问题,请参考以下文章

Python+pandas+matplotlib数据分析与可视化案例

在 Pandas/Matplotlib 上输入图例后命名堆积条

如何在 pandas/matplotlib 中绘制索引列?

如何在 pandas / matplotlib 中绘制多条线

Python pandas / matplotlib在条形图列上方注释标签[重复]

numpy pandas matplotlib