pandas 为直方图熊猫设置轴标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42832675/
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-09-14 03:12:38  来源:igfitidea点击:

Setting axis labels for histogram pandas

pythonpandasmatplotlib

提问by horizoncrying

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

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

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.

我正在通过 groubpy 绘制两个直方图。我希望每个子图都具有相同的 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")

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

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

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

Screenshot of my histograms

我的直方图的屏幕截图

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

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

采纳答案by FLab

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