简体   繁体   English

Python绘图条形图汇总列,由两个不同的列分组

[英]Python Plot Bar Chart Summing a Column, Grouped by Two Different Columns

I have a data set: 我有一个数据集:

Year Month Category Count Line
2019   1       a      2    5
2019   2       b      5    7
2018   1       a      7    12

I want to plot a bar chart in chronological order where the y axis is a sum of the Count field and there is a separate secondary axis for the Line column (no sum). 我想按时间顺序绘制条形图,其中y轴是Count字段的总和,并且Line列有一个单独的辅助轴(无和)。

I am having trouble creating that group and sum. 我无法创建该组并总结。

Try: 尝试:

df['SumCount'] = df.groupby('Year')['Count'].transform('sum')
ax=df.plot(kind='bar', x='Year',y='Line', alpha=.7, color='g', position=1, width =.2)
df.plot(kind='bar', x='Year',y='SumCount', ax=ax, secondary_y=True, alpha=.8, position=0, width=.2)

Output: 输出:

在此输入图像描述

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

相关问题 Python DataFrame - plot 带有分组列(至少两列)的数据框条形图 - Python DataFrame - plot a bar chart for data frame with grouped-by columns (at least two columns) 如何在python中绘制两个选定列的分组条形图与时间的关系图。 我将在下面发布我的数据 - How to plot grouped bar chart from two selected columns vs time in python. I shall post my data below 在 python 中使用 matplotlib 绘制多个分组条形图 - Plot multiple grouped bar chart with matplotlib in python python 中的条形图按性别列分组 - bar chart in python grouped by the sex column 如何从 dataframe 到 plot 在 x 中包含多个列的分组条形图? - How to plot a grouped bar chart from a dataframe with several columns in x? 如何使用电子表格中的两个不同列制作按图分组的条形图? - How to make a plotly grouped bar chart using two different columns from spreadsheet? 条形 plot 用于按第三列分组的两个数据列 - Bar plot for two data columns grouped by a third columns 如何在python中绘制具有多个y轴的分组条形图 - How to plot grouped bar chart with multiple y axes in python plotly 按一列的值分组的许多列的堆积条形图(熊猫) - Stacked Bar chart of many columns grouped by the values of one column (Pandas) Python 可视化 2 个按列和年份分组的图中的条形图 - Python visualisation 2 Bar charts in one plot grouped by Column and year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM