【问题标题】:Create a bar plot and pie plot with 3 columns创建具有 3 列的条形图和饼图
【发布时间】:2020-05-02 10:54:26
【问题描述】:

我的数据框在下面

  Month  Sales2015  Sales2016
0    Q3       0.00   13208.52
1    Q4   10500.78   23114.91
2    Q2       0.00    6627.00
3    Q1   19881.00   13254.00
4    Q3    3684.48       0.00

我的代码在下面

sa[['Month','Sales2015','Sales2016']].plot(kind='bar')

但是这个图表是不正确的。我需要

  • 在条形图中比较 2015 年和 2016 年的季度销售额

  • 使用 Month 和 Sales2016 创建饼图

【问题讨论】:

  • 您要按月和年添加销售额,然后为每个月创建图表吗?

标签: pandas matplotlib


【解决方案1】:

我将使用GroupBy.sum

df.groupby('Month').sum().plot(kind='bar')
#print(df.groupby('Month').first())

#       Sales2015  Sales2016
#Month                      
#Q1      19881.00   13254.00
#Q2          0.00    6627.00
#Q3          0.00   13208.52
#Q4      10500.78   23114.91


df.groupby('Month').sum().plot.pie(subplots=(1,len(df.columns[1:])),figsize=(10,10))

#array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f1303f64a20>,
#       <matplotlib.axes._subplots.AxesSubplot object at 0x7f1303f29048>],
#      dtype=object)

【讨论】:

  • 你能解释一下吗for i,ax in enumerate(axs): agg_df.plot.pie(y=list_sales[i],ax=ax)
  • 使用 plt.subplots 生成两个轴来绘制图形,并通过循环对其进行引用。但是你可以使用我刚刚发布的第二种解决方案
  • 如何在字符上也获取值,例如https://stackoverflow.com/questions/41088236/how-to-have-actual-values-in-matplotlib-pie-chart-displayed-python
猜你喜欢
  • 1970-01-01
  • 2012-01-08
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2013-06-17
  • 1970-01-01
相关资源
最近更新 更多