【问题标题】:density distribution and bar plot from x and y data来自 x 和 y 数据的密度分布和条形图
【发布时间】:2021-07-14 05:43:35
【问题描述】:

我在 pandas 数据框中有以下数据集:

x = df_data.iloc[:,0].values
y = df_data.iloc[:,1].values

以下数据分别在x和y中:

x = 30, 31, 32, 33, 34, 35, 36
y = 1000, 2000, 3000, 4000, 3000, 2000, 1000

y 表示计数(每个 x 值存在的频率)。

我现在想用密度分布线制作条形图。我愿意使用 seaborn 或 matplotlib,但找不到分别输入 x 和 y 数据并获得条形图和密度图的方法。

我试过了:

x = [30,31,32,33,34,35,36]
y = [1000, 2000, 3000, 4000, 3000, 2000, 1000]
##
sns.distplot(x, hist=True, kde=True,
    bins=int(150/150), color='darkblue',
    hist_kws={'edgecolor':'black'},
    kde_kws={'linewidth': 4})
plt.show()

但没有得到我想要的。

我想要下面的东西(仅用于我的数据)

(我的这张图片来自:https://towardsdatascience.com/histograms-and-density-plots-in-python-f6bda88f5ac0

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    首先,请注意 distplot 在 Seaborn 0.11 中已贬值。扩展和改进版本现在称为 histplot(带有可选 kde 的直方图)、kdeplot(仅用于 kde)和 displot(创建子图)。

    可选的weights= 参数设置每个x 值的权重。 discrete=True 需要为每个 x 值设置一个条形图。 kde 的cut 参数控制曲线在数据点之外绘制的距离。

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    x = [30, 31, 32, 33, 34, 35, 36]
    y = [1000, 2000, 3000, 4000, 3000, 2000, 1000]
    
    sns.histplot(x=x, weights=y, discrete=True,
                 color='darkblue', edgecolor='black',
                 kde=True, kde_kws={'cut': 2}, line_kws={'linewidth': 4})
    plt.show()
    

    请注意,如果基础数据是连续的,则通过提供原始数据可以获得更正确的图。

    要更改 kde 行的颜色,一个明显的想法是使用line_kws={'color': 'red'},但这在当前的 seaborn 版本 (0.11.1) 中不起作用。

    但是,您可以分别绘制histplotkdeplot。为了匹配 y 轴,histplot 需要stat='density'(默认为'count')。

    ax = sns.histplot(x=x, weights=y, discrete=True, alpha=0.5,
                      color='darkblue', edgecolor='black', stat='density')
    sns.kdeplot(x=x, weights=y, color='crimson', cut=2, linewidth=4, ax=ax)
    

    另一种方法是在之后更改线条的颜色,它独立于所选的stat=

    ax = sns.histplot(x=x, weights=y, discrete=True,
                 color='darkblue', edgecolor='black',
                 kde=True, kde_kws={'cut': 2}, line_kws={'linewidth': 4})
    ax.lines[0].set_color('crimson')
    

    这是一个示例,如何将一个数据集的直方图与另一个数据集的 kde 曲线结合起来:

    import matplotlib.pyplot as plt
    from matplotlib.ticker import MultipleLocator
    import seaborn as sns
    
    x = [30, 31, 32, 33, 34, 35, 36]
    y = [1000, 2000, 3000, 4000, 3000, 2000, 1000]
    x2 = [20, 21, 22, 23, 24, 25, 26]
    y2 = [1000, 2000, 3000, 4000, 3000, 2000, 1000]
    
    ax = sns.histplot(x=x2, weights=y2, discrete=True, alpha=0.5,
                      color='darkblue', edgecolor='black', stat='density')
    sns.kdeplot(x=x, weights=y, color='crimson', cut=2, linewidth=4, ax=ax)
    ax.xaxis.set_major_locator(MultipleLocator(1))
    plt.show()
    

    【讨论】:

    • 太棒了。现在是否有可能将蓝线的颜色更改为红色,但将直方图的颜色保持为蓝色?
    • 谢谢!最后一个问题:如果我现在想绘制这条红线,对于上面没有直方图的图中的数据,而是将另一组数据的直方图添加到图中,即:x2 = [20,21,22 的直方图, 23, 24, 25, 26] 和 y2 = [1000, 2000, 3000, 4000, 3000, 2000, 1000],加上红线 x = [30, 31, 32, 33, 34, 35, 36] y = [1000、2000、3000、4000、3000、2000、1000]。这可能吗?
    • 你需要histplot,不带kde,但需要stat='density',然后是单独的kdeplot。请注意,如果没有 stat='density' 直方图,两个数据集的比例将完全不同。
    猜你喜欢
    • 2020-04-20
    • 1970-01-01
    • 2019-02-13
    • 2014-07-07
    • 2015-10-12
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多