Pandas Matplotlib在dataframes中绘制具有2列关系的条形图
我一直在绘制数据帧。 这可能很简单,但我搞不懂 我有如下熊猫数据帧记录:Pandas Matplotlib在dataframes中绘制具有2列关系的条形图,pandas,dataframe,matplotlib,pyspark,Pandas,Dataframe,Matplotlib,Pyspark,我一直在绘制数据帧。 这可能很简单,但我搞不懂 我有如下熊猫数据帧记录: Year occurrence Count 0 2011 0 306 1 2011 1 1838 2 2012 0 422 3 2012 1 1816 4 2013 0 423 5 2013 1 3471 6 2014
Year occurrence Count
0 2011 0 306
1 2011 1 1838
2 2012 0 422
3 2012 1 1816
4 2013 0 423
5 2013 1 3471
6 2014 0 537
7 2014 1 3239
8 2015 0 993
9 2015 1 7668
10 2016 0 415
11 2016 1 2052
12 2017 0 511
13 2017 1 4750
14 2018 0 705
15 2018 1 2125
我想把这个数据框绘制成条形图,这样,x轴包含年份,Y轴包含计数。
有人能解决这个问题吗?
我已经根据我的结果创建了示例df:
我试过这样做:
pdf_1.plot(x='Year', y=['Count'], kind='bar')
但它并没有给我我想要的东西。你可以用它来重塑:
pdf_1.pivot('Year', 'occurrence', 'Count').plot.bar(stacked=True)
输出:
您可以使用来重塑:
pdf_1.pivot('Year', 'occurrence', 'Count').plot.bar(stacked=True)
输出:
根据@BigBen
我已经找到了所有三个答案:
# For Question 1
pdf_1.pivot(index='Year', columns='above_threshold', values='Count').plot.bar()
# For Question 2
pdf_1.pivot('Year', 'above_threshold', 'Count').plot.bar(stacked=True)
# For Question 3
pdf_1.pivot(index='Year', columns='above_threshold', values='Count').plot.line()
根据@BigBen
我已经找到了所有三个答案:
# For Question 1
pdf_1.pivot(index='Year', columns='above_threshold', values='Count').plot.bar()
# For Question 2
pdf_1.pivot('Year', 'above_threshold', 'Count').plot.bar(stacked=True)
# For Question 3
pdf_1.pivot(index='Year', columns='above_threshold', values='Count').plot.line()
谢谢@BigBen,快速回答!另外,你知道我如何在同样的条件下简单地绘制直线和条形图(每年2条)吗?谢谢@BigBen,快速回答!另外,你知道我如何在同样的条件下简单地绘制直线和条形图(每年2条)吗?