简体   繁体   English

Python:在单个图中绘制多列

[英]Python : Plot multiple columns in a single graph

df contains multiple columns and the three columns I need to plot are df 包含多列,我需要绘制的三列是

gen_1    gen_2    gen_3

M          M        M
F          M        M
F          F        F
F          F        M
F          M        F

To plot them in different graph在不同的图表中绘制它们

df.groupby('gen_1')['pid'].nunique().plot(kind='bar')
plt.show()
df.groupby('gen_2')['pid'].nunique().plot(kind='bar')
plt.show()
df.groupby('gen_3')['pid'].nunique().plot(kind='bar')
plt.show()

But I need them in a single graph to compare.但我需要将它们放在一个图表中进行比较。

I never use the pandas plotting tools.我从不使用熊猫绘图工具。 Plotting from pandas works fine, I just prefer to stick to my routine.从熊猫绘图工作正常,我只是更喜欢坚持我的例行公事。 I create figures manually, so this is one option.我手动创建图形,所以这是一种选择。 Here is an example:下面是一个例子:

import pylab as pl

fig = pl.figure(figsize=(3.25, 2.5))
ax0 = fig.add_subplot(111)

ax0.bar(df.index, df.groupby('gen_1')['pid'].nunique())
ax0.bar(df.index, df.groupby('gen_2')['pid'].nunique())
ax0.bar(df.index, df.groupby('gen_3')['pid'].nunique())

fig.show()

pl.savefig("filename.png", bbox_inches="tight", dpi=500)
pl.close("all")

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

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