【问题标题】:Add title to pandas histogram using "by" argument使用“by”参数为 pandas 直方图添加标题
【发布时间】:2013-02-07 07:22:37
【问题描述】:

如果使用“by”参数,如何在 Series 上使用 pandas hist 函数创建的直方图添加标题?

对于简单的直方图没有问题。因此,以下工作完美:

In [115]: runELMLearning.utime.hist()
Out[115]: <matplotlib.axes.AxesSubplot at 0x20aaa590>

In [116]: plt.title("fooo")
Out[116]: <matplotlib.text.Text at 0x228b8550>

但是,当使用 by 子句时,这不再起作用:

In [117]: runELMLearning.utime.hist(by=runELMLearning.state)
Out[117]: 
array([[Axes(0.1,0.763636;0.222222x0.136364),
        Axes(0.388889,0.763636;0.222222x0.136364),
        Axes(0.677778,0.763636;0.222222x0.136364)],
       [Axes(0.1,0.559091;0.222222x0.136364),
        Axes(0.388889,0.559091;0.222222x0.136364),
        Axes(0.677778,0.559091;0.222222x0.136364)],
       [Axes(0.1,0.354545;0.222222x0.136364),
        Axes(0.388889,0.354545;0.222222x0.136364),
        Axes(0.677778,0.354545;0.222222x0.136364)],
       [Axes(0.1,0.15;0.222222x0.136364),
        Axes(0.388889,0.15;0.222222x0.136364),
        Axes(0.677778,0.15;0.222222x0.136364)]], dtype=object)

In [118]: plt.title("fooo")
Out[118]: <matplotlib.text.Text at 0x13612b10>

即使它没有表示任何错误,标题也会被默默地忽略。

我的另一个奇怪观察是,当我先创建一个图形,然后使用 hist 函数时,在 by 参数的情况下,第一个图形被忽略并且 hist 函数创建一个新的图形,我找不到matplotlib 的 gca。对于简单的直方图也没有问题。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    好的,这是我自己发现的:我需要使用 pyplot 中的 suptitle 为整个图形而不是单个轴添加标题,子图中的不同直方图就是这种情况。

    http://matplotlib.1069221.n5.nabble.com/title-when-using-subplot-td22151.html

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 2021-12-05
      • 2015-09-04
      • 2015-12-17
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      相关资源
      最近更新 更多