设置直方图 pandas 的轴标签 [英] Setting axis labels for histogram pandas

查看:93
本文介绍了设置直方图 pandas 的轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此还很陌生,所以对此可能有一个非常明显的答案.我很抱歉!

I'm fairly new to this, so there might be a very obvious answer to this. My apologies!

我正在通过古怪的方式绘制两个直方图.我希望每个子图具有相同的x和y标签以及相同的标题.我知道 sharex=True 可以解决问题,但如果我只在 df.hist 之后设置轴,显然不会.我尝试过各种设置xlabels的版本,但现在迷路了.

I'm plotting two histograms via a groubpy. I'd like my subplots to each have the same x and y labels and a common title. I understood that sharex=True would do the trick, but apparently not if I set the axis only after the df.hist. I've tried various versions of setting the xlabels and am lost now.

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")

所以我最终得到的是仅用于子图的标签.

So what I end up with is labels only for the subplot.

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

知道如何解决这个问题吗?(必须使用pandas/python.)

Any idea on how to solve this? (Have to use pandas/python.)

推荐答案

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

Labels are properties of axes objects, that needs to be set on each of them. Here's an example that worked for me:

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")

这篇关于设置直方图 pandas 的轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆