如何对已经分组的栏进行分组?
How to group already grouped bar?
假设我的 pandas 数据框如下所示:
import pandas as pd
df=pd.DataFrame({'run-1a':[0.3, 0.3, 0.4, 0.4], 'run-1b':[0.3, 0.3, 0.4, 0.5],"run-2a":[0.7, 0.9, 0.8, 0.9],"run-2b":[0.2, 0.3, 0.5, 0.5], "Person":["person1","person2","person3","person4"]})
df
现在我用它做了一个酒吧:
import matplotlib.pyplot as plt
color_list = ['b','lightskyblue', 'g','lightgreen']
ax = df.plot(x='Person',y=['run-1a','run-1b','run-2a','run-2b'], kind='bar', color=color_list)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation="horizontal")
plt.show()
结果如下所示:
不过我想 运行-1a 与 运行1b 和 运行2a 与 运行2b 每个人。当我用谷歌搜索一些解决方案时,我只发现如何使分组条形图看起来像我上面的图,我不需要。
这是我的情节的草图:
是否可以选择如何将其分组每个人,请问?
非常感谢。
如果我没理解错的话,你可以在其他条之间添加空条,这样你就会得到 4 列,每两个条是分开的。
import pandas as pd
df=pd.DataFrame({'':[0,0,0,0],'run-1a':[0.3, 0.3, 0.4, 0.4], 'run-1b':[0.3, 0.3, 0.4, 0.5],"run-2a":[0.7, 0.9, 0.8, 0.9],"run-2b":[0.2, 0.3, 0.5, 0.5], "Person":["person1","person2","person3","person4"]})
color_list = ['b','lightskyblue','w', 'g','lightgreen']
ax = df.plot(x='Person',y=['run-1a','run-1b','','run-2a','run-2b'], kind='bar', color=color_list)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation="horizontal")
plt.show()
假设我的 pandas 数据框如下所示:
import pandas as pd
df=pd.DataFrame({'run-1a':[0.3, 0.3, 0.4, 0.4], 'run-1b':[0.3, 0.3, 0.4, 0.5],"run-2a":[0.7, 0.9, 0.8, 0.9],"run-2b":[0.2, 0.3, 0.5, 0.5], "Person":["person1","person2","person3","person4"]})
df
现在我用它做了一个酒吧:
import matplotlib.pyplot as plt
color_list = ['b','lightskyblue', 'g','lightgreen']
ax = df.plot(x='Person',y=['run-1a','run-1b','run-2a','run-2b'], kind='bar', color=color_list)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation="horizontal")
plt.show()
结果如下所示:
不过我想 运行-1a 与 运行1b 和 运行2a 与 运行2b 每个人。当我用谷歌搜索一些解决方案时,我只发现如何使分组条形图看起来像我上面的图,我不需要。
这是我的情节的草图:
是否可以选择如何将其分组每个人,请问? 非常感谢。
如果我没理解错的话,你可以在其他条之间添加空条,这样你就会得到 4 列,每两个条是分开的。
import pandas as pd
df=pd.DataFrame({'':[0,0,0,0],'run-1a':[0.3, 0.3, 0.4, 0.4], 'run-1b':[0.3, 0.3, 0.4, 0.5],"run-2a":[0.7, 0.9, 0.8, 0.9],"run-2b":[0.2, 0.3, 0.5, 0.5], "Person":["person1","person2","person3","person4"]})
color_list = ['b','lightskyblue','w', 'g','lightgreen']
ax = df.plot(x='Person',y=['run-1a','run-1b','','run-2a','run-2b'], kind='bar', color=color_list)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation="horizontal")
plt.show()