根据接受的答案here,使用subplots 创建Figure 和axis 对象实例。
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')
注意,df1.hist()中没有赋值关键字参数sharex、sharey和layout,有利于在@987654338中设置sharex、sharey、nrows和ncols @ 实现类似的效果。重要的元素是将df.hist() 的关键字参数ax 分配给先前初始化的axes 对象。标题可以用suptitle设置。