【问题标题】:pandas histogram in matplotlib framwork with date resamplematplotlib框架中的熊猫直方图与日期重采样
【发布时间】:2020-05-20 04:11:57
【问题描述】:

我创建了一个包含每小时数据和三列的数据框。它看起来像:

                           A       B            C
2017-01-01 00:00:00  14417.5900  15252.8700  15252.8700
2017-01-01 01:00:00   1754.4800  14639.5600  14639.5600
2017-01-01 02:00:00     64.2600     14.7900     14.7900
2017-01-01 03:00:00     50.5789    486.8810    486.8810
2017-01-01 04:00:00      7.2784      8.6431      8.6431
2017-01-01 05:00:00    444.1068     28.7847     28.7847

我想创建一个条形直方图,以按月比较模型 A、B 和 C。 因此,我将数据汇总为:

df = df.resample('M').sum() 

此时我被卡住了。我已经尝试过两种熊猫功能,例如

df = df.resample('M').sum().hist()

但是我得到了三个不同的数字。此外,我想转向 matplotlib 框架,我可以在其中个性化输出。

欢迎提出任何建议。

真的非常感谢

【问题讨论】:

    标签: python pandas date matplotlib histogram


    【解决方案1】:

    我想你想要 DataFrame.plotkind = 'bar'

    df.resample('M').sum().plot(kind = 'bar')
    

    DataFrame.plot.bar

    df.resample('M').sum().plot.bar()
    

    我们也可以使用:

    df.groupby([df.index.month,df.index.year]).sum().plot.bar()
    

    【讨论】:

    • 亲爱的,亲爱的 ansev,感谢您的回答。有用。在 matplotlib 框架中可以做到这一点吗?这将允许我个性化情节。再次感谢。
    • @diedro : 您可以访问 df plot 返回的轴对象并使用它来个性化绘图。
    【解决方案2】:

    这就是我处理问题的方式:

     bar_width = 0.2
     x = np.arange(12)
    
     y1 = df1.loc[:,'A'].values[:]
     y2 = df1.loc[:,'B'].values[:]
     y3 = df1.loc[:,'C'].values[:]
    
     plt.bar(x,             y1, bar_width,color=colf[0], label=labf[0])
     plt.bar(x+bar_width,   y2, bar_width,color=colf[1], label=labf[1])
     plt.bar(x+2*bar_width, y3, bar_width,color=colf[2], label=labf[2])
    

    之后,

       x_ticklabels = [r'\textbf{Jan}', r'\textbf{Feb}', r'\textbf{Mar}',r'\textbf{Apr}',
                        r'\textbf{May}', r'\textbf{Jun}', r'\textbf{Jul}',r'\textbf{Ago}',
                        r'\textbf{Sep}', r'\textbf{Oct}', r'\textbf{Nov}',r'\textbf{Dec}']
    
    plt.xticks(x, x_ticklabels, rotation=30)
    
    

    虽然不是很笼统,但至少在我看来效果还不错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-10
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多