add title to collection of pandas hist plots

匿名 (未验证) 提交于 2019-12-03 08:35:02

问题:

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:

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

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

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

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?

回答1:

You can use 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") 


回答2:

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.



回答3:

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!