Python 为熊猫历史图集合添加标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19614400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:10:48  来源:igfitidea点击:

add title to collection of pandas hist plots

pythonpandastitlehistogram

提问by dreme

I'm looking for advice on how to show a title at the top of a collection of histogram plots that have been generated by a pandas df.hist() command. For instance, in the histogram figure block generated by the code below I'd like to place a general title (e.g. 'My collection of histogram plots') at the top of the figure:

我正在寻找有关如何在由 pandas df.hist() 命令生成的直方图集合顶部显示标题的建议。例如,在由下面的代码生成的直方图块中,我想在图的顶部放置一个通用标题(例如“我的直方图集合”):

data = DataFrame(np.random.randn(500).reshape(100,5), columns=list('abcde'))
axes = data.hist(sharey=True, sharex=True)

I've tried using the titlekeyword in the hist command (i.e. title='My collection of histogram plots'), but that didn't work.

我试过在 hist 命令中使用title关键字(即 title='我的直方图集合'),但这不起作用。

The following code doeswork (in an ipython notebook) by adding text to one of the axes, but is a bit of a kludge.

下面的代码工作(在IPython的笔记本型)通过将文本添加到所述轴中的一个,但是有点组装机的。

axes[0,1].text(0.5, 1.4,'My collection of histogram plots', horizontalalignment='center',
               verticalalignment='center', transform=axes[0,1].transAxes)

Is there a better way?

有没有更好的办法?

采纳答案by HYRY

You can use suptitle():

您可以使用suptitle()

import pylab as pl
from pandas import *
data = DataFrame(np.random.randn(500).reshape(100,5), columns=list('abcde'))
axes = data.hist(sharey=True, sharex=True)
pl.suptitle("This is Figure title")

回答by E.Wang

I found a better way:

我找到了一个更好的方法:

plt.subplot(2,3,1)  # if use subplot
df = pd.read_csv('documents',low_memory=False)
df['column'].hist()
plt.title('your title')

It is very easy, display well at the top, and will not mess up your subplot.

这很容易,在顶部显示得很好,并且不会弄乱您的子图。

回答by Filippo Mazza

With newer Pandas versions, if someone is interested, here a slightly different solution with Pandas only:

对于较新的 Pandas 版本,如果有人感兴趣,这里仅使用 Pandas 的解决方案略有不同:

ax = data.plot(kind='hist',subplots=True,sharex=True,sharey=True,title='My title')

回答by CrepeGoat

for matplotlib.pyplot, you can use:

对于matplotlib.pyplot,您可以使用:

import matplotlib.pyplot as plt
# ...
plt.suptitle("your title")

or if you're using a Figureobject directly,

或者如果你Figure直接使用一个对象,

import matplotlib.pyplot as plt
fig, axs = plt.subplots(...)
# ...
fig.suptitle("your title")

See this example.

请参阅此示例