来自两个 pandas 数据框的分组条形图 [英] Grouped bar chart from two pandas data frames

查看:61
本文介绍了来自两个 pandas 数据框的分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个包含不同值但结构相同的数据帧:

I have two data frames containing different values but the same structure:

df1 =

         0         1         2         3         4 
D  0.003073  0.014888  0.155815  0.826224       NaN
E  0.000568  0.000435  0.000967  0.002956  0.067249  

df2 =

     0         1         2         3         4
D  0.746689  0.185769  0.060107  0.007435       NaN   
E  0.764552  0.000000  0.070288  0.101148  0.053499

我想在单个分组的条形图中绘制两个数据框.另外,每一行(索引)都应该是一个子图.

I want to plot both data frames in a single grouped bar chart. In addition, each row (index) should be a subplot.

对于其中一个熊猫,可以直接使用熊猫轻松实现:

This can be easily achieved for one of them using pandas directly:

df1.T.plot(kind="bar", subplots=True, layout=(2,1), width=0.7, figsize=(10,10), sharey=True)

我尝试使用

pd.concat([df1, df2], axis=1)

这将导致一个新的数据框:

which results in a new dataframe:

         0         1         2         3         4         0         1         2         3         4
D  0.003073  0.014888  0.155815  0.826224       NaN  0.746689  0.185769  0.060107  0.007435       NaN
E  0.000568  0.000435  0.000967  0.002956  0.067249  0.764552  0.000000  0.070288  0.101148  0.053499

但是,使用上述方法绘制数据框不会将每列的条形分组,而是将它们分开处理.对于每个子图,这会导致x轴具有按列顺序重复的刻度线,例如0,1,2,3,4,0,1,2,3,4.

However, plotting the data frame with the above method will not group the bars per column but rather treats them separately. Per subplot this results in a x-axis with duplicated ticks in order of the columns, e.g. 0,1,2,3,4,0,1,2,3,4.

有什么想法吗?

推荐答案

目前尚不清楚数据的组织方式.熊猫和海洋生物通常期望整洁的数据集.因为您确实在绘制之前转置了数据,所以我假设您有两个变量(A和B)和四个观测值(例如测量值)

It is not exactly clear how the data is organized. Pandas and seaborn usually expect tidy datasets. Because you do transpose the data prior to plotting I assume you have two variable (A and B) and four observations (e.g. measurements)

df1 = pd.DataFrame.from_records(np.random.rand(2,4), index = ['A','B'])
df2 = pd.DataFrame.from_records(np.random.rand(2,4), index = ['A','B'])

df1.T

也许这接近您想要的:

df4 = pd.concat([df1.T, df2.T], axis=0, ignore_index=False)
df4['col'] = (len(df1.T)*(0,) + len(df2.T)*(1,))
df4.reset_index(inplace=True)
df4

使用seaborns刻面网格可以方便地进行绘制:

using seaborns facet grid allows for convenient plotting:

sns.factorplot(x='index', y='A', hue='col', kind='bar', data=df4)

这篇关于来自两个 pandas 数据框的分组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆