[英]Python & Pandas: Plotting a Pivot with multiple Indexes
Hi to all the experts,各位专家您好,
I'm new to Python and Data Science and actually I'm learning with a real world example to get into Data Science.我是 Python 和数据科学的新手,实际上我正在通过一个真实世界的例子来学习数据科学。
I loaded already a CSV and did some work on the data.我已经加载了一个 CSV 并对数据进行了一些处理。 That's ok.
没关系。 I have the following dataframe:
我有以下数据框:
Then, I created a Pivot from the dataframe:然后,我从数据框中创建了一个 Pivot:
pivot = pd.pivot_table(
data=df,
index=['Category', 'month', 'year'],
values='Amount',
aggfunc='sum',
margins=True)
Now, I have the following dataframe:现在,我有以下数据框:
Now, I want to plot the following (line chart or bar chart):现在,我想绘制以下内容(折线图或条形图):
But, I want that for explicit Category like "Business" to see, how the amount changed over the periods.但是,我希望对于像“业务”这样的明确类别,可以看到金额在一段时间内是如何变化的。
Whats the best way, to plot a clear, beautiful chart with matplotlib?用 matplotlib 绘制清晰、漂亮的图表的最佳方法是什么?
Thanks in Advance.提前致谢。
Many Greetings Leon许多问候莱昂
You can use the below code to build the graphs.您可以使用以下代码来构建图表。 I think the stacked bar graphs would be a good way to see the
Amount
in each month.我认为堆积条形图是查看每个月
Amount
的好方法。
Code代码
## Add AFTER you have created your pivot table
dfg = pivot.reset_index().set_index(['Month', 'Category']).sort_index(level=[0,1])
fig, ax = plt.subplots(figsize=(6,4))
dfg['Amount'].unstack().plot.bar(stacked=True, ax=ax, legend = False)
ax.set_xticklabels(sorted(df.Month.unique()), rotation=0)
ax.set_title('My Graph')
fig.legend(loc="upper right", bbox_to_anchor=(1.1, 0.9))
plt.show()
Stacked Bar graph堆积条形图
Unstacked Bar graph未堆叠条形图
Change stacked = True
to stacked = False
to see the bars next to each other, if you are not a fan of stacked bars如果您不喜欢堆叠条形图,请将
stacked = True
更改为stacked = False
以查看彼此相邻的条形图
Line Graphs You can also use line graphs, but not my personal preference.折线图你也可以使用折线图,但不是我个人的偏好。 Replace the
plot.bar
line in above code to将上面代码中的
plot.bar
行替换为
dfg['Amount'].unstack().plot(kind='line', marker='o', ax=ax, legend = False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.