如何在 histplot 中标记核密度估计

huangapple go评论57阅读模式
英文:

How to label the kernel density estimate in histplot

问题

代码部分不需要翻译,以下是你要翻译的内容:

I am plotting a histogram using seaborn, along with a KDE curve and a Gaussian fit, but the instruction label = "KDE fit" in sns.histplot is inappropriately displayed in color, as it seems to refer to the whole histogram... Is there a way to specifically label the KDE curve so as to appear in the legend box as a solid green line (just as the Gaussian fit appears as a dashed red line)?

我正在使用seaborn绘制直方图,同时使用KDE曲线和高斯拟合,但在sns.histplot中的label = "KDE fit"指令不正确地显示为颜色,似乎是指整个直方图... 是否有一种方法可以特别标记KDE曲线,使其以实线绿色线的形式显示在图例框中(就像高斯拟合以虚线红色线的形式显示)?

The full code I used is below:

我使用的完整代码如下:

import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
import scipy.stats as stats
from scipy.stats import norm

# Generating data

np.random.seed(63123)
data = np.random.normal(loc=600, scale=30, size=20)

# Parameters for histogram plotting

min_val = data.min()
max_val = data.max()
val_width = max_val - min_val
n_bins = 7
bin_width = val_width / n_bins

list_xticks_raw = np.arange(min_val - bin_width/2, max_val + bin_width/2, bin_width).tolist()
list_xticks_round = [round(x) for x in list_xticks_raw]

# Histogram and Gaussian fit plotting

fig = plt.figure(figsize=(4, 4))

h = sns.histplot(data=None, x=data, bins=n_bins, binrange=(min_val, max_val), discrete=False, shrink=1.0,
                 stat="density", element="bars", color="green", kde=True, label="KDE fit")

plt.xlim(min_val - bin_width/2, max_val + bin_width/2)  # Define x-axis limits
plt.xticks(list_xticks_round)

mu, sigma = stats.norm.fit(data)
sorted_data = np sort(data)
gaussian_fit = stats.norm.pdf(sorted_data, mu, sigma)
plt.plot(sorted_data, gaussian_fit, linestyle="--", color="red", label="Gaussian fit")

plt.legend()

plt.show()

如何在 histplot 中标记核密度估计


[![在此输入图像描述][1]][1]

<details>
<summary>英文:</summary>

I am plotting a histogram using `seaborn`, along with a KDE curve and a Gaussian fit, but the instruction `label = &quot;KDE fit&quot;` in `sns.histplot` is inappropriately displayed in color, as it seems to refer to the whole histogram... Is there a way to specifically label the KDE curve so as to appear in the `legend` box as a solid green line (just as the Gaussian fit appears as a dashed red line)?

The full code I used  is below:

    import numpy as np
    import matplotlib.pylab as plt
    import seaborn as sns
    import scipy.stats as stats
    from scipy.stats import norm

    # Generating data
    
    np.random.seed(63123) 
    data = np.random.normal(loc = 600, scale = 30, size = 20)

    # Parameters for histogram plotting
    
    min_val = data.min()
    max_val = data.max()
    val_width = max_val - min_val
    n_bins = 7
    bin_width = val_width/n_bins
    
    list_xticks_raw = np.arange(min_val - bin_width/2, max_val + bin_width/2, bin_width).tolist()
    list_xticks_round = [round(x) for x in list_xticks_raw]

    # Histogram and Gaussian fit plotting
    
    fig = plt.figure(figsize = (4,4))
    
    h = sns.histplot(data = None, x = data , bins = n_bins, binrange=(min_val, max_val), discrete = False, shrink = 1.0,
                 stat = &quot;density&quot;, element = &quot;bars&quot;, color = &quot;green&quot;, kde = True, label = &quot;KDE fit&quot;)
    
    plt.xlim(min_val - bin_width/2, max_val + bin_width/2) # Define x-axis limits
    plt.xticks(list_xticks_round)
    
    mu, sigma = stats.norm.fit(data)
    sorted_data = np.sort(data)
    gaussian_fit = stats.norm.pdf(sorted_data, mu, sigma)
    plt.plot(sorted_data, gaussian_fit, linestyle = &quot;--&quot;, color = &quot;red&quot;, label = &quot;Gaussian fit&quot;)
    
    plt.legend()
    
    plt.show()

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/x7Dzb.png

</details>


# 答案1
**得分**: 1

使用`line_kws`参数代替`label`:

```python
h = sns.histplot(data=None, x=data, bins=n_bins, binrange=(min_val, max_val), discrete=False, shrink=1.0,
                 stat="density", element="bars", color="green", kde=True, line_kws={'label': "KDE fit"})

输出:

如何在 histplot 中标记核密度估计

英文:

Use the line_kws parameter instead of label:

h = sns.histplot(data = None, x = data , bins = n_bins, binrange=(min_val, max_val), discrete = False, shrink = 1.0,
             stat = &quot;density&quot;, element = &quot;bars&quot;, color = &quot;green&quot;, kde = True, line_kws={&#39;label&#39;: &quot;KDE fit&quot;})

Output:

如何在 histplot 中标记核密度估计

huangapple
  • 本文由 发表于 2023年3月7日 23:07:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定