Python 中带箱线图的直方图

Histogram with Boxplot above in Python

您好,我想绘制一个直方图,箱线图出现在直方图的顶部,显示 Q1、Q2 和 Q3 以及异常值。下面是示例 phone。 (我正在使用 Python 和 Pandas)

我已经使用 matplotlib.pyplot 检查了几个例子,但很难找到一个好的例子。我还想让直方图曲线如下图所示。

我也尝试了 seaborn,它为我提供了形状线和直方图,但没有找到与上面的 boxpot 合并的方法。

任何人都可以帮助我在 matplotlib.pyplot 上或使用 pyplot

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="ticks")

x = np.random.randn(100)

f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, 
                                    gridspec_kw={"height_ratios": (.15, .85)})

sns.boxplot(x, ax=ax_box)
sns.distplot(x, ax=ax_hist)

ax_box.set(yticks=[])
sns.despine(ax=ax_hist)
sns.despine(ax=ax_box, left=True)

扩展@mwaskom 的答案,我做了一个小的自适应函数。

import seaborn as sns
def histogram_boxplot(data, xlabel = None, title = None, font_scale=2, figsize=(9,8), bins = None):
    """ Boxplot and histogram combined
    data: 1-d data array
    xlabel: xlabel 
    title: title
    font_scale: the scale of the font (default 2)
    figsize: size of fig (default (9,8))
    bins: number of bins (default None / auto)

    example use: histogram_boxplot(np.random.rand(100), bins = 20, title="Fancy plot")
    """

    sns.set(font_scale=font_scale)
    f2, (ax_box2, ax_hist2) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)}, figsize=figsize)
    sns.boxplot(data, ax=ax_box2)
    sns.distplot(data, ax=ax_hist2, bins=bins) if bins else sns.distplot(data, ax=ax_hist2)
    if xlabel: ax_hist2.set(xlabel=xlabel)
    if title: ax_box2.set(title=title)
    plt.show()

histogram_boxplot(np.random.randn(100), bins = 20, title="Fancy plot", xlabel="Some values")

Image

def histogram_boxplot(feature, figsize=(15,10), bins=None):
    f,(ax_box,ax_hist)=plt.subplots(nrows=2,sharex=True, gridspec_kw={'height_ratios':(.25,.75)},figsize=figsize)                                  
                                                                                                   
    sns.distplot(feature,kde=False,ax=ax_hist,bins=bins) 
    sns.boxplot(feature,ax=ax_box, color='Red')
    ax_hist.axvline(np.mean(feature),color='g',linestyle='-')
    ax_hist.axvline(np.median(feature),color='y',linestyle='--')

仅使用 matplotlib 的解决方案,只是因为:

# start the plot: 2 rows, because we want the boxplot on the first row
# and the hist on the second
fig, ax = plt.subplots(
    2, figsize=(7, 5), sharex=True,
    gridspec_kw={"height_ratios": (.3, .7)}  # the boxplot gets 30% of the vertical space
)

# the boxplot
ax[0].boxplot(data, vert=False)

# removing borders
ax[0].spines['top'].set_visible(False)
ax[0].spines['right'].set_visible(False)
ax[0].spines['left'].set_visible(False)

# the histogram
ax[1].hist(data)

# and we are good to go
plt.show()