简体   繁体   English

如何将两列熊猫数据框绘制为水平条形图?

[英]How to plot two columns of pandas dataframe as horizontal barplot?

I am trying to compare between 2 columns in my dataframe, and want to plot them as a horizontal barplot using Seaborn. 我正在尝试在数据框中的2列之间进行比较,并希望使用Seaborn将其绘制为水平条形图。 In the plot below is just one column plotted ('budget2019'). 在下面的图中,仅绘制了一个列(“ budget2019”)。

仅用一年的“ budget2019”绘制的实际图

1) How can I plot the second column ('budget2018') next to it, so that one can better see the evolution between both columns' values, and compare visually both bars, for each "ministry"? 1)如何绘制第二列(“ budget2018”)旁边的内容,以便每个“部委”都能更好地看到两列值之间的变化,并直观地比较两个条形?

2) How can I put the x-value at the end of each bar, so one can see (read) the x-value on each bar, and hasn't to go with the eyes down, and try to approximate the value? 2)如何将x值放在每个小节的末尾,这样就可以看到(读取)每个小节上的x值,而不必视线为准,并尝试近似该值? Because now on the actual plot one can't really read the x-value directly from a bar. 因为现在在实际绘图上,不能真正从条形图上直接读取x值。 (pretty difficult to get the exact value when looking at the plot) (在查看绘图时很难获得确切的值)

3) After I get it to plot the 'budget2018' bars next to 'budget2019' bars, is there a way to also put the evolution in percent on the plot? 3)在我将其绘制在“ budget2019”条旁边的“ budget2018”条之后,是否有办法将演变百分比也绘制在该图上? So that it's visually better readable, how much evolution in % between both columns is? 为了使它在视觉上更具可读性,两列之间的百分比差异是多少?

Here is a look at my dataframe: 这是我的数据框:

我的小数据框的一部分

And here is the code I'm using to plot for now: 这是我现在要绘制的代码:

plt.figure(figsize=(15,8))

sns.set(style="darkgrid")

#ax = sns.barplot(x="budget2018", y="ministere", data=budget, label="Total")
sns.set_color_codes("pastel")
sns.barplot(x="budget2019", y="ministere", data=budget, label="Budget 2019")

sns.despine(left=True, bottom=True)

plt.tight_layout()
plt.show()

PS: If it is not doable with Seaborn, a solution with Matplotlib alone would be also okay. PS:如果不能使用Seaborn,则仅使用Matplotlib解决方案也是可以的。 And what is needed, are horizonal bars, as in my plot, otherwise the yticks aren't readable. 就像我的情节一样,需要的是水平条,否则yticks无法读取。

EDIT (after using the code in ImportanceOfBeingErnest's comment): 编辑(使用ImportanceOfBeingErnest注释中的代码后):

Here is the plot I am actually seeing, after @ImportanceOfBeingErnest commented. 这是@ImportanceOfBeingErnest评论后我实际看到的情节。 It is pretty close to what I need. 它非常接近我的需求。

@ImportanceOfBeingErnest的评论后的情节(设置索引)

To use seaborn, you have to use an intermediate dataframe created using melt to produce a "long-form" dataframe. 要使用seaborn,您必须使用通过melt创建的中间数据框以生成“长格式”数据框。

df2 = pd.melt(budget, id_vars=['ministere'], value_vars=['budget2018','budget2019'], var_name='year')

then plotting of both column is done using the hue= parameter: 然后使用hue=参数完成两列的绘制:

fig,ax = plt.subplots()
sns.set_color_codes("pastel")
sns.barplot(x="value", y="ministere", hue="year", data=df2, palette='pastel')

adjusting the labels as requested is fairly easy (and you'll find plenty of other exaples on SO): 根据要求调整标签是相当容易的(您会在SO上找到很多其他实例):

for i,m in budget.iterrows():
    ax.annotate(s='{:.2f}%'.format(m.loc['evolution_percent']),
                xy=(m.loc[['budget2018','budget2019']].max(),i),
                xytext=(10,0),
                textcoords='offset pixels',
                ha='left',
                va='center'
               )

labels = ['{:s}\n(2019: {:.2f}€)'.format(d.ministere,d.budget2019) for _,d in budget.iterrows()]
ax.set_yticklabels(labels)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM