Python 可视化同一数据集中的两列
我试图对四列进行分组和排序,计算数值,并在同一条形图中绘制它们,以查看计数的变化趋势Python 可视化同一数据集中的两列,python,matplotlib,bar-chart,plotly,data-visualization,Python,Matplotlib,Bar Chart,Plotly,Data Visualization,我试图对四列进行分组和排序,计算数值,并在同一条形图中绘制它们,以查看计数的变化趋势 Year Month Bl_year Month 2018 Jan 2019 Jan 2018 Feb 2018 Mar 2018 Dec 2020 Dec 2019 Apr 2019 Sep 2020 Nov 2020 Dec 2019 Sep 2018 Jan 我试着先进行分组和排
Year Month Bl_year Month
2018 Jan 2019 Jan
2018 Feb 2018 Mar
2018 Dec 2020 Dec
2019 Apr 2019 Sep
2020 Nov 2020 Dec
2019 Sep 2018 Jan
我试着先进行分组和排序,先按年份计算价值,然后按月份计算价值
df_Activity_count = df.sort_values(['year','month'],ascending = True).groupby('month')
df_Activity_count_BL = df.sort_values(['BL year','BL month'],ascending = True).groupby('BL month')
现在我试着在同一个酒吧里比较这两个。请有人帮忙。尝试将
ax
传递给绘图命令:
df_Activity_count = df.sort_values(['year','month'],ascending = True).groupby('month')
df_Activity_count_BL = df.sort_values(['BL year','BL month'],ascending = True).groupby('BL month')
ax = df_Activity_count.years.value_counts().unstack(0).plot.bar()
df_Activity_count_BL['BL year'].value_counts().unstack(0).plot.bar(ax=ax)
由于您标记了
matplotlib
,因此我将使用pyplot
import matplotlib.pyplot as plt
# Create an axis object
fig, ax = plt.subplots()
# Define dataframes
df_Activity_count = df.sort_values(['year','month'],ascending = True).groupby('month')
df_Activity_count_BL = df.sort_values(['BL year','BL month'],ascending = True).groupby('BL month')
# Plot using the axis object ax defined above
df_Activity_count['year'].value_counts().unstack(0).plot.bar(ax=ax)
df_Activity_count_BL['BL year'].value_counts().unstack(0).plot.bar(ax=ax)
这两种代码给了我相同的解决方案。非常感谢你的解决方案,今天学到了一些新东西。但是寻找类似的东西。我想把2019年4月和2019年4月的计数互相比较,两个条形图挨在一起,这样我就可以直观地看到差异Hi Sheldore,谢谢你的帮助。我接受了你的回答。我投了赞成票。你应该点击答案旁边的灰色复选标记,你会发现这两个答案中最好的。你不能投票,因为你的声誉很低,但你可以接受的复选标记