使用 matplotlib 有多种方法可以实现此目的。
plt.subplots()
方法以及AxesGrid1
和gridspec
工具包都提供了非常优雅的解决方案,但可能需要时间来学习。
执行此操作的一种简单、强力的方法是自己手动将轴对象添加到图形中。
import numpy as np
import matplotlib.pyplot as plt
# fake data
x = np.random.lognormal(mean=2.25, sigma=0.75, size=37)
# setup the figure and axes
fig = plt.figure(figsize=(6,4))
bpAx = fig.add_axes([0.2, 0.7, 0.7, 0.2]) # left, bottom, width, height:
# (adjust as necessary)
histAx = fig.add_axes([0.2, 0.2, 0.7, 0.5]) # left specs should match and
# bottom + height on this line should
# equal bottom on bpAx line
# plot stuff
bp = bpAx.boxplot(x, notch=True, vert=False)
h = histAx.hist(x, bins=7)
# confirm that the axes line up
xlims = np.array([bpAx.get_xlim(), histAx.get_xlim()])
for ax in [bpAx, histAx]:
ax.set_xlim([xlims.min(), xlims.max()])
bpAx.set_xticklabels([]) # clear out overlapping xlabels
bpAx.set_yticks([]) # don't need that 1 tick mark
plt.show()
是的,我见过的处理此问题的最佳方法可以在这里找到。 代码和图表的副本:
# Import library and dataset
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')
# Cut the window in 2 parts
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)})
# Add a graph in each part
sns.boxplot(df["sepal_length"], ax=ax_box)
sns.distplot(df["sepal_length"], ax=ax_hist)
# Remove x axis name for the boxplot
ax_box.set(xlabel='')
我一直在寻找类似的东西,这对我有用。图片链接在这里https://raw.githubusercontent.com/preetihemant/preetihemant.github.io/master/images/hist_boxplot.png
plt.subplot(2,1,1)
plt.hist(data,histtype='bar',bins=[values])
plt.xlabel('x-label')
plt.ylabel('y-label')
plt.title('Graph title')
plt.subplot(2,1,2)
plt.boxplot(values)
plt.show()
以下是为实现完整数据集的结果而修改的函数:
def histogram_boxplot(df, figsize=(16,4)):
cols = df.columns
rows = int(np.ceil(len(cols)/4))
f,axes = plt.subplots(2*rows,4, sharex=True,
gridspec_kw={'height_ratios':(.2,.8)},
figsize=figsize)
r,c = 0,0
for feature in cols:
if df[feature].dtype == 'object':
continue
sns.boxplot(df[feature],ax=axes[r,c], vert=False, color='Skyblue')
sns.histplot(df[feature],kde=True, ax=axes[r+1,c])
axes[r+1,c].axvline(np.mean(df[feature]),color='g',linestyle='-')
axes[r+1,c].axvline(np.median(df[feature]),color='r',linestyle='--')
axes[r ,c].set_xticklabels([]) # clear out overlapping xlabels
axes[r ,c].set_yticks([]) # don't need that 1 tick mark
c = c+1
if c == 4:
c = 0
r = r+2
plt.plot()
return