python - Pandas 直方图标签和标题

标签 python pandas histogram

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

df1.hist(column='human_den',by='region',sharex=True,sharey=True,layout=(1,3))

最佳答案

在接受的答案here之后,使用 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')

请注意,未在sharex中分配关键字参数shareylayoutdf1.hist(),而是建议在sharex中设置shareynrowsncolsplt.subplots以获得类似的效果。重要的元素是将df.hist()的关键字参数ax分配给先前初始化的axes对象。可以使用suptitle设置标题。

Shared x and shared y common label

关于python - Pandas 直方图标签和标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685185/

相关文章:

python - 如何使 Seaborn 直方图具有细条/箱

python - 你如何在 Python/Django/MySQL 中避免这种竞争条件?

python - 在 Windows 7 上安装 JDK 和 Python 并设置 PATH 变量

pandas - Pandas Dataframe 中的分组和连接列

python - 如何连接/使用字符串在 python/pandas 中执行命令

python - 在python中绘制颜色直方图

matlab - 在 Matlab 中并排绘制直方图

python - 导入错误 : No module named numpy on spark workers

python - 在Python中使用数组以更方便的方式编写长switch语句

python - Pandas DataFrame 到列表列表