用大熊猫绘制图-将图分成单独的每月图

时间:2020-06-25 04:49:27

标签: python pandas plot

我一直在与Pandas一起研究整个数据管理方面的内容,现在我正试图绘制结果。我的代码有点乱,我的变量名看起来像是胡说八道(不好的编码礼节,对不起),但是我一直在尝试很多不同的方法,但有点卡住了。这是我所拥有的:

    Output from xxP.columns:
Index(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
       'Nov', 'Dec'],
      dtype='object')

Output from xxP.head:
<bound method NDFrame.head of         Jan    Feb   Mar   Apr    May  ...    Aug    Sep   Oct   Nov   Dec
Year                                   ...                                
2010    0.0    4.8  27.8  30.6   89.8  ...   76.1   33.6  20.6  10.8   9.5
2011   21.6    0.0   0.2  21.7   85.0  ...   96.6   89.9  35.4  24.2  51.8
2012   35.2   14.6   0.0  57.0   92.0  ...   94.2   70.4  12.8  46.8  59.0
2013   10.3    2.8  53.2  29.8  135.0  ...  127.3  145.2  34.8   7.8   1.0
2014    0.4    0.0  13.2  17.8  123.0  ...   67.2   66.8  41.3  20.7   0.2
2015    3.2   15.8  20.3  44.1   66.3  ...  106.6   45.5  24.6  42.7  15.0
2016   21.0   12.8  14.3  55.3  121.8  ...  143.0   91.2  32.9  28.0  11.0
2017    5.8  115.3  25.8   0.0   66.1  ...  169.2   79.7  23.5  24.3  28.8
2018  124.4    0.3   5.6  31.9   57.9  ...  150.4   26.9  44.4   5.1   3.0
2019    4.6    0.8  12.0  31.9   25.2  ...  113.6   34.7  32.8  13.3   2.3
2020    2.1   11.0  40.9  11.1  107.3  ...    NaN    NaN   NaN   NaN   NaN

[11 rows x 12 columns]>

我正在尝试绘制数据,以便可以查看和比较每年。运行简单的xxP.plot(y ='Rain','kind ='bar')时,我得到以下信息:

Graph output

格式很好,值也合适,但是有没有办法根据月份将它们分开?这将产生12个独立的图形,每个月一个,x轴为年份。我以前在绘图时使用了groupby函数,但不确定在这种情况下如何执行此操作,这就是为什么我包括了xxP.columns输出的原因。

谢谢!

1 个答案:

答案 0 :(得分:1)

  • 通过遍历月度列,将每个月的图绘制到单独的图上
import pandas as pd
import matplotlib.pyplot as plt

# data
data = {'Year': [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020],
        'Jan': [0.0, 21.6, 35.2, 10.3, 0.4, 3.2, 21.0, 5.8, 124.4, 4.6, 2.1],
        'Feb': [4.8, 0.0, 14.6, 2.8, 0.0, 15.8, 12.8, 115.3, 0.3, 0.8, 11.0],
        'Mar': [27.8, 0.2, 0.0, 53.2, 13.2, 20.3, 14.3, 25.8, 5.6, 12.0, 40.9],
        'Apr': [30.6, 21.7, 57.0, 29.8, 17.8, 44.1, 55.3, 0.0, 31.9, 31.9, 11.1],
        'May': [89.8, 85.0, 92.0, 135.0, 123.0, 66.3, 121.8, 66.1, 57.9, 25.2, 107.3],
        'Aug': [76.1, 96.6, 94.2, 127.3, 67.2, 106.6, 143.0, 169.2, 150.4, 113.6, nan],
        'Sep': [33.6, 89.9, 70.4, 145.2, 66.8, 45.5, 91.2, 79.7, 26.9, 34.7, nan],
        'Oct': [20.6, 35.4, 12.8, 34.8, 41.3, 24.6, 32.9, 23.5, 44.4, 32.8, nan],
        'Nov': [10.8, 24.2, 46.8, 7.8, 20.7, 42.7, 28.0, 24.3, 5.1, 13.3, nan],
        'Dec': [9.5, 51.8, 59.0, 1.0, 0.2, 15.0, 11.0, 28.8, 3.0, 2.3, nan]}

# setup dataframe
df = pd.DataFrame(data)

# display
    Year    Jan    Feb   Mar   Apr    May    Aug    Sep   Oct   Nov   Dec
0   2010    0.0    4.8  27.8  30.6   89.8   76.1   33.6  20.6  10.8   9.5
1   2011   21.6    0.0   0.2  21.7   85.0   96.6   89.9  35.4  24.2  51.8
2   2012   35.2   14.6   0.0  57.0   92.0   94.2   70.4  12.8  46.8  59.0
3   2013   10.3    2.8  53.2  29.8  135.0  127.3  145.2  34.8   7.8   1.0
4   2014    0.4    0.0  13.2  17.8  123.0   67.2   66.8  41.3  20.7   0.2
5   2015    3.2   15.8  20.3  44.1   66.3  106.6   45.5  24.6  42.7  15.0
6   2016   21.0   12.8  14.3  55.3  121.8  143.0   91.2  32.9  28.0  11.0
7   2017    5.8  115.3  25.8   0.0   66.1  169.2   79.7  23.5  24.3  28.8
8   2018  124.4    0.3   5.6  31.9   57.9  150.4   26.9  44.4   5.1   3.0
9   2019    4.6    0.8  12.0  31.9   25.2  113.6   34.7  32.8  13.3   2.3
10  2020    2.1   11.0  40.9  11.1  107.3    NaN    NaN   NaN   NaN   NaN

# plot each month
for col in df.columns[1:]:
    plt.figure()
    plt.bar(x='Year', height=col, data=df)
    plt.title(col)
    plt.show()

如果要改为子图

plt.figure(figsize=(16, 10))
for i, col in enumerate(df.columns[1:], 1):
    plt.subplot(4, 3, i)
    plt.bar(x='Year', height=col, data=df)
    plt.ylim(0, df.iloc[:, 1:].max().max())
    plt.title(col)
    plt.tight_layout()
plt.show()

enter image description here