多个条形图matplotlib的单个图例

时间:2017-01-26 13:10:25

标签: 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

中的一个分类器模型 - 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

3 个答案:

答案 0 :(得分:5)

嗯,首先,你的桌子格式不整齐(见这里: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

然后,只需在一行+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

答案 1 :(得分:1)

您可以使用以下内容将图例移动到图表中所需的位置。

绘制条形图时添加标签是必要的。我已经改变了你绘制传奇的主线。

我添加了一些虚拟标签,在您的代码中,您可以通过labels = list(df)获取标签,为您提供数据框中列名的列表。

import matplotlib.pyplot as plt

colorscheme = ['r','b','c','y']
fig1 = plt.figure()
accuracyFig = fig1.add_subplot(221)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
    accuracyFig.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])

accuracyFig1 = fig1.add_subplot(223)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
    accuracyFig1.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])

accuracyFig2 = fig1.add_subplot(222)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
    accuracyFig2.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])

accuracyFig3 = fig1.add_subplot(224)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
    accuracyFig3.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])

# Plot the legend:
# You don't want to plot to any particular axis, instead to a general plot.

plt.legend(loc = 'lower center',bbox_to_anchor = (0,-0.3,1,1),
        bbox_transform = plt.gcf().transFigure)
plt.show()

传奇情节的来源:

How to create custom legend in matplotlib based on the value of the barplot? how do I make a single legend for many subplots with matplotlib? How to put the legend out of the plot

enter image description here

更新:意外删除了我的评论:在ncol = 2中添加legend()将提供您想要的对称拆分行为。

答案 2 :(得分:1)

我修改了以下代码

fig1 = plt.figure()

A = list(range(1,len(dframe)+1))
labels = dframe['name'].tolist()

colorscheme = sns.color_palette(n_colors=len(dframe))


accuracyFig = fig1.add_subplot(221)
for i in range(0,len(A)):
    accuracyFig.bar(A[i],dframe['accuracy'][i+1], align = 'center',label = labels[i], color = colorscheme[i])
accuracyFig.set_xticks([])
accuracyFig.set_ylim([0.5,1])
accuracyFig.set_title('Accuracy')

sensitivityFig = fig1.add_subplot(222)
for i in range(0,len(A)):
    sensitivityFig.bar(A[i],dframe['sensitivity'][i+1], align = 'center',label = labels[i], color = colorscheme[i])
sensitivityFig.set_xticks([])
sensitivityFig.set_ylim([0.5,1])
sensitivityFig.set_title('Sensitivity')

specificityFig = fig1.add_subplot(223)
for i in range(0,len(A)):
    specificityFig.bar(A[i],dframe['specificity'][i+1], align = 'center',label = labels[i], color = colorscheme[i])
specificityFig.set_xticks([])
specificityFig.set_ylim([0.5,1])
specificityFig.set_title('Specificity')

precisionFig = fig1.add_subplot(224)
for i in range(0,len(A)):
    precisionFig.bar(A[i],dframe['precision'][i+1], align = 'center',label = labels[i], color = colorscheme[i])
precisionFig.set_xticks([])
precisionFig.set_ylim([0.5,1])
precisionFig.set_title('Precision')

# Plot the legend:

plt.legend(loc = 'lower center',bbox_to_anchor = (0,-0.05,1,2), ncol=2,
        bbox_transform = plt.gcf().transFigure)

plt.show()

我没有使用固定长度的标签,而是直接从数据框中复制它们并且它可以正常工作。

我做了一些更新,并且还将参数(n_cols = 2)添加到图例功能中,以便我的输出图像看起来像这样 enter image description here

感谢@Charles Morris的帮助