我有一个使用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数据帧.这给我输出了下图.
每种颜色对应于一个分类器模型 - perceptron,C2,C3 and C4
存储在熊猫中dframe['name']
现在我想为整个人物绘制一个单一的图例.我尝试了以下内容
leg = plt.legend(dframe['name'])
有关如何绘制单个图例并将其放在2个列中的图形的任何帮助.
但它给了我以下内容.
这是我的数据框架
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
jrjc.. 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
HTH
嗯,首先,你的桌子不是一个整洁的格式(见这里: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
HTH