设置直方图pandas的轴标签

问题描述 投票:5回答:1

我对此很新,所以可能会有一个非常明显的答案。我很抱歉!

我正在通过一个集合绘制两个直方图。我希望我的每个子图都有相同的x和y标签以及一个共同的标题。我明白sharex = True会做的伎俩,但显然不是我只在df.hist之后设置轴。我已经尝试了各种版本的设置xlabels并且现在丢失了。

import pylab as pl
from pandas import *

histo_survived = df.groupby('Survived').hist(column='Age', sharex=True, sharey=True)
pl.title("Histogram of Ages")
pl.xlabel("Age")
pl.ylabel("Individuals")

所以我最终得到的只是子图的标签。

Out: <matplotlib.text.Text at 0x11a27ead0>

关于如何解决这个问题的任何想法? (必须使用pandas / python。)

python pandas matplotlib
1个回答
5
投票

标签是轴对象的属性,需要在每个对象上设置。这是一个对我有用的例子:

frame = pd.DataFrame([np.random.rand(20), np.sign(np.random.rand(20) - 0.5)]).T
frame.columns = ['Age', 'Survived']

# Note that you can let the hist function do the groupby
# the function hist returns the list of axes created
axarr = frame.hist(column='Age', by = 'Survived', sharex=True, sharey=True, layout = (2, 1))

for ax in axarr.flatten():
    ax.set_xlabel("Age")
    ax.set_ylabel("Individuals")
© www.soinside.com 2019 - 2024. All rights reserved.