在X轴上按年份绘制图表

问题描述 投票:0回答:1

我需要打印一个条形图或任何其他图表,X轴上的年份和Y轴上的自杀人数。

[Sample DataFrame[1]

(df.pivot_table(
    index=df.year,
    columns='suicides_no',
    aggfunc='size').plot(kind='bar', stacked=True, colormap='dark2', width=0.2)
)

但它并没有打印出我想要的东西。例如,单个颜色的条形图有不同的值。

enter image description here

有什么其他的方法来打印图形吗?或者有什么方法可以完美地设置条形图的颜色编码?

pandas matplotlib analytics data-analysis
1个回答
0
投票

当应用pivot_table时,你的索引需要被重新设置,以便在你的情节中使用。cat 的变量,你想把它堆叠在

df.pivot_table(
    values='suicides_no',
    index='year',
    columns='cat',
    aggfunc=np.sum).reset_index().plot(x='year',kind='bar', stacked=True)
© www.soinside.com 2019 - 2024. All rights reserved.