python - 多个条形图的单个图例 matplotlib

标签 python pandas matplotlib dataframe seaborn

我使用 pandas Dataframe 中的不同列创建了多个条形图。

fig1 = plt.figure()
ypos = np.arange(len(dframe))

colorscheme = seaborn.color_palette(n_colors=4)

accuracyFig = fig1.add_subplot(221)
accuracyFig.bar(ypos,dframe['accuracy'], align = 'center', color=colorscheme)
accuracyFig.set_xticks([0,1,2,3])
accuracyFig.set_ylim([0.5,1])

sensitivityFig = fig1.add_subplot(222)
sensitivityFig.bar(ypos, dframe['sensitivity'], align = 'center',color=colorscheme )
sensitivityFig.set_xticks([0,1,2,3])
sensitivityFig.set_ylim([0.5,1])

specificityFig = fig1.add_subplot(223)
specificityFig.bar(ypos, dframe['specificity'], align = 'center', color=colorscheme)
specificityFig.set_xticks([0,1,2,3])
specificityFig.set_ylim([0.5,1])

precisionFig = fig1.add_subplot(224)
precisionFig.bar(ypos, dframe['precision'], align = 'center', color=colorscheme)
precisionFig.set_xticks([0,1,2,3])
precisionFig.set_ylim([0.5,1])

哪里dframe是一个具有整数值的 Pandas 数据框。这会输出下图 enter image description here .

每种颜色对应一个分类器模型 - perceptron,C2,C3 and C4存储在 Pandas 中的 dframe['name']

现在我想为整个图形绘制一个图例。我尝试了以下

leg = plt.legend(dframe['name'])

任何有关如何绘制单个图例并将其放置在 2 列中的图形的帮助。

但它给了我以下 enter image description here .

这是我的数据框

                     name        accuracy     sensitivity     specificity       precision
0              perceptron  0.820182164169  0.852518881235  0.755172413793  0.875007098643
1  DecisionTreeClassifier             1.0             1.0             1.0             1.0
2    ExtraTreesClassifier             1.0             1.0             1.0             1.0
3  RandomForestClassifier  0.999796774253  0.999889340748  0.999610678532  0.999806362379

最佳答案

首先,您的表格格式不整齐(参见此处:http://vita.had.co.nz/papers/tidy-data.pdf)。

让你的表格整齐(或长)的格式有一个巨大的优势,那就是使用 seaborn 绘图变得非常容易(还有其他优势):

df # yours
                     name        accuracy     sensitivity     specificity       precision
0              perceptron  0.820182164169  0.852518881235  0.755172413793  0.875007098643
1  DecisionTreeClassifier             1.0             1.0             1.0             1.0
2    ExtraTreesClassifier             1.0             1.0             1.0             1.0
3  RandomForestClassifier  0.999796774253  0.999889340748  0.999610678532  0.999806362379

将其转换为长格式(或整洁):

df2 = pd.melt(df, value_vars=["accuracy", "sensitivity", "specificity", "precision"], id_vars="name")
df2
                      name     variable     value
0               perceptron     accuracy  0.820182
1   DecisionTreeClassifier     accuracy  1.000000
2     ExtraTreesClassifier     accuracy  1.000000
3   RandomForestClassifier     accuracy  0.999797
4               perceptron  sensitivity  0.852519
5   DecisionTreeClassifier  sensitivity  1.000000
6     ExtraTreesClassifier  sensitivity  1.000000
7   RandomForestClassifier  sensitivity  0.999889
8               perceptron  specificity  0.755172
9   DecisionTreeClassifier  specificity  1.000000
10    ExtraTreesClassifier  specificity  1.000000
11  RandomForestClassifier  specificity  0.999611
12              perceptron    precision  0.875007
13  DecisionTreeClassifier    precision  1.000000
14    ExtraTreesClassifier    precision  1.000000
15  RandomForestClassifier    precision  0.999806

然后,只需在 1 行 + 2 行中绘制您想要的内容以使其更清晰:

g = sns.factorplot(data=df2,
                   kind="bar",
                   col="variable", # you have 1 plot per variable, forming 1 line and 4 columns (4 different variables)
                   x="name", # in each plot the x-axis will be the name
                   y="value", # the height of the bar
                   col_wrap=2) # you actually want your line of plots to contain 2 plots maximum 
g.set_xticklabels(rotation=90) # rotate the labels so they don't overlap
plt.tight_layout() # fit everything into the figure

multiple barplot

HTH

关于python - 多个条形图的单个图例 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41874184/

相关文章:

python django 1.4 如何设置两个 WWW-Authenticate http header

python - 如何从Python的行列表中选择特定元素

python - Pandas Dataframe 检索唯一列

python-3.x - matplotlib 中如何使用 zorder?

python - Python 中的异或实现给出了错误的预测

python - Pandas 将两个单独的列转换为单个日期时间列?

python - 将绘图标签添加到 Seaborn Lineplot

python - 用于动画构建的 OpenCV 或 matplotlib?

python - 如何删除 matplotlib.pyplot 中子图之间的空格?

python - 在 matplotlib 中放大插图而不重新绘制数据