【问题标题】:Histogram with Boxplot above in PythonPython中带有上面Boxplot的直方图
【发布时间】:2016-01-27 15:39:51
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python matplotlib histogram boxplot seaborn


    【解决方案1】:
    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)
    

    【讨论】:

    • 谢谢!正是我正在寻找的方式。但它带来了一些问题,列之间存在间隙,我尝试使用bins 参数但没有运气。此外,我希望 y 轴具有 frequency 而不是概率。我在seaborn 文档中没有找到任何方法来做到这一点。你能帮忙吗?
    • 这些与您的原始问题完全不同,因此您应该将它们作为两个新问题提出。
    • 别担心伙计们。经过一段时间的搜索,我实际上找到了解决方案。感谢您的陪伴。
    • 知道如何将上述样式的 8 个独立实例组合成一个图吗?
    【解决方案2】:

    扩展@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

    【讨论】:

      【解决方案3】:
      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='--')
      

      【讨论】:

      • 请修复您的代码格式并提供一些背景信息,说明您的解决方案优于提供的其他答案的原因。
      猜你喜欢
      • 2013-09-13
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2017-07-01
      • 2013-10-13
      • 2018-04-18
      相关资源
      最近更新 更多