熊猫直方图标签和标题

DJV*_*DJV 6 python histogram pandas

我试图将x轴和y轴标签以及标题放在我通过Pandas创建的三面板直方图上,但似乎无法正确放置.我在标题中得到的唯一结果和三个图中最后一个的x轴标签.我想要一个整体标题,xlabel和ylabel.制作情节的代码如下.有什么建议?

df1.hist(column='human_den',by='region',sharex=True,sharey=True,layout=(1,3))
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 11

此处接受的答案开始,使用subplots创建Figureaxis对象实例.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# random data
df1 = pd.DataFrame(columns=['human_den','region'])
df1['human_den'] = np.random.rand(100)
df1['region'] = np.random.choice(['Northeast', 'South', 'Midwest'], size=100)

# set up figure & axes
fig, axes = plt.subplots(nrows=1, ncols=3, sharex=True, sharey=True)

# drop sharex, sharey, layout & add ax=axes
df1.hist(column='human_den',by='region', ax=axes)

# set title and axis labels
plt.suptitle('Your Title Here', x=0.5, y=1.05, ha='center', fontsize='xx-large')
fig.text(0.5, 0.04, 'common X', ha='center')
fig.text(0.04, 0.5, 'common Y', va='center', rotation='vertical')
Run Code Online (Sandbox Code Playgroud)

请注意,关键字参数sharex,sharey以及layout未分配的df1.hist(),有利于制定的sharex,sharey,nrowsncolsplt.subplots实现类似的效果.重要的元素是将df.hist()关键字参数赋值给ax先前初始化的axes对象.标题可以设置为suptitle.

共享x和共享y公共标签