python - pandas 绘制一个特定的列作为 x 轴和 y 轴?

标签 python pandas plot

我有一个如下所示的数据框:

我想将company_id绘制为x轴,将count(company_id)绘制为y轴。 我还想按 openclose 类别堆叠它。

代码不显示任何输出并无限运行。

df
        person_id  company_id   time    event   type    date
    0          1    255      1379312026 open    A   2013-09-16 02:13:46
    1          1    255      1379312086 close   A   2013-09-16 02:14:46
    2          1    182      1379312926 open    B   2013-09-16 02:28:46
    3          1    182      1379313046 close   B   2013-09-16 02:30:46
    4          1    81       1379314006 open    A   2013-09-16 02:46:46


df2=df[['company_id','event']]
df2.plot(kind='bar',stacked=True)

最佳答案

如果您能够按照评论中的建议使用seaborn,您可以执行以下操作:

import seaborn as sns, matplotlib.pyplot as plt

df = {'company_id': {0: 255, 1: 255, 2: 182, 3: 182, 4: 81}, 
      'time': {0: 1379312026, 1: 1379312086, 2: 1379312926, 3: 1379313046, 4: 1379314006}, 
      'person_id': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1}, 
      'counts': {0: 2, 1: 2, 2: 2, 3: 2, 4: 1}, 
      'type': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'A'}, 
      'event': {0: 'open', 1: 'close', 2: 'open', 3: 'close', 4: 'open'}}

# add counts column    
counts = df.groupby('company_id').size().rename('counts')
df['count'] = df['company_id'].map(counts)

g = sns.factorplot(y='count',x='company_id',hue='event',data=df,kind='bar',
                   palette='muted',legend=False,ci=None)
plt.legend(loc='best')
plt.show()

结果:

enter image description here

关于python - pandas 绘制一个特定的列作为 x 轴和 y 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38860428/

相关文章:

python - 为什么从实例获取类属性会引发 AttributeError?

python - 如何使用 dateutil.tz.tz.tzoffset 来本地化使用 strptime 创建的时区原始日期时间?

python - 导入前 PyCharm 打印?

python - BioPython:氨基酸序列包含 'J',无法计算分子量

python - elasticbeanstalk gcc 和 python-devel 安装

python-2.7 - 如何将通过 "pandas.describe"获取的参数一次性放入一个图中?

python-3.x - 使用 pandas 在两个条件下创建列

r - 修改边距,使多个绘图在 R 中具有相同的大小

image - 数学 : Transparent background with PNG

python - 将颜色列表传递给 pandas .p​​lot