【问题标题】:Can someone please help me make my overlapping histograms?有人可以帮我制作重叠直方图吗?
【发布时间】:2021-12-26 03:13:32
【问题描述】:

我有以下格式的三个数据框,其中有一列以数字格式显示一年中的月份,而与之相邻的一列则显示该月发生的项目数。我想创建一个重叠直方图,详细说明三个直方图之间的分布,但由于某种原因,我一直得到同样的结果!

    month_box   Sum Value
0     1        4812
1     2        2053
2     3        2405
3     4        2353
4     5        2427
5     6        2484
6     8        2579
7     9        2580
8    10        2497
9    11        2510
10   12        2202

我使用的代码如下:

sns.distplot(bex_boxdf['month_box'],kde=False,label = 'Bexley')
sns.distplot(west_boxdf['month_box'],kde=False,label = 'Westminster')
sns.distplot(gwch_boxdf['month_box'],kde=False,label = 'Greenwich')
plt.legend(prop={'size': 12})
plt.title('Crime by month')
plt.xlabel('Month')
plt.ylabel('Density')

我在我得到的结果下方附上...帮助将不胜感激,谢谢。

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    使用 Matplotlib 的直方图

    以下是使用@Esa 提供的数据的三个不同视图。

    还有一个“阶梯式”直方图类型,我没有包括在内,但根据数据的分布情况可能会有用:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({'month_box': [1,2,3,4,5,6,7,8,9,10,11,12],
                   'Bexley': [4812,2053,2405,2353,2427,2484,2579,2580,
                                 2497,2510,2202,2021],
                   'Westminster': [4712,2050,2435,2323,2487,2414,2679,2780,
                                 2490,2110,2702,2022],
                   'Greenwich': [4812,2053,2405,2353,2427,2484,2579,2580,
                                 2497,2510,2202,2021],
                   })
    
    data = df["Bexley"], df["Westminster"], df["Greenwich"]
    labels = ["Bexley", "Westminster", "Greenwich"]
    fig, ax = plt.subplots(ncols=3, sharex=True, sharey=True)
    ax[0].hist(x=data, histtype="bar",label=labels)
    ax[1].hist(x=data, histtype="barstacked",label=labels)
    ax[2].hist(x=data, histtype="step", label=labels)
    plt.legend()
    plt.show()
    

    Matplotlib 有很多自定义选项。参考文档可能很有用。 https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html

    使用 Seaborn 的直方图

    此外,如果您更喜欢使用 Seaborn,这里有一个简单的示例,其中包括一条平滑线(使用内核密度估计,又名 kde)

    import seaborn as sns
    sns.histplot(data, multiple="layer", kde=True)
    

    https://seaborn.pydata.org/generated/seaborn.histplot.html

    【讨论】:

      【解决方案2】:

      有时直接从数据框中绘制或使用 matplotlib 代替 seaborn 更容易。而其他时候 seaborn 更好,所以最好至少在某种程度上学习两者。

      如果您首先将数据排列到一个数据框中,这是一个简单的解决方案。您没有提供其他两个数据框,因此我设置了一些值作为示例。

      df = pd.DataFrame({'month_box': [1,2,3,4,5,6,7,8,9,10,11,12], 
                     'Bexley_sum': [4812,2053,2405,2353,2427,2484,2579,2580,
                                   2497,2510,2202,2021],
                     'Westminster_sum': [4712,2050,2435,2323,2487,2414,2679,2780,
                                   2490,2110,2702,2022],                   
                     'Greenwich_sum': [4812,2053,2405,2353,2427,2484,2579,2580,
                                   2497,2510,2202,2021],                   
                     })
      
      df.plot(x='month_box', y=['Bexley_sum', 'Westminster_sum', 'Greenwich_sum'], kind='bar')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-25
        • 1970-01-01
        • 2022-06-14
        • 2018-12-10
        • 1970-01-01
        • 2021-05-30
        相关资源
        最近更新 更多