【问题标题】:Get actual numbers instead of normalized value in seaborn KDE plots在 seaborn KDE 图中获取实际数字而不是标准化值
【发布时间】:2017-10-02 04:19:44
【问题描述】:

我有三个数据框,我在 python 中使用 seaborn 模块绘制 KDE。问题是这些图试图使曲线 1 下的区域(这是它们的预期执行方式),因此图中的高度是标准化的。但是有什么方法可以显示实际值而不是标准化值。还有什么方法可以找出曲线的交点?

注意:我不想使用 scipy 的 curve_fit 方法,因为我不确定每个数据帧的分布,它也可以是多模式的。

import seaborn as sns
plt.figure()
sns.distplot(data_1['gap'],kde=True,hist=False,label='1')
sns.distplot(data_2['gap'],kde=True,hist=False,label='2')
sns.distplot(data_3['gap'],kde=True,hist=False,label='3')
plt.legend(loc='best')
plt.show()

代码的输出附在链接中,因为我无法发布图片。plot_link

【问题讨论】:

  • 在这种情况下“实际值”是什么意思?
  • @mwaskom 这里的实际值是指该值的频率。但曲线应该是平滑的,这意味着如果一个值的频率为 0,但当前值之前和之后的值的频率很高,就不应该有波谷。

标签: python scipy seaborn kernel-density


【解决方案1】:

你可以抓住这条线并用set_data重新调整它的y值:

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

# create some data
n = 1000
x = np.random.rand(n)

# plot stuff
fig, ax = plt.subplots(1,1)
ax = sns.distplot(x, kde=True, hist=False, ax=ax)

# find the line and rescale y-values
children = ax.get_children()
for child in children:
    if isinstance(child, matplotlib.lines.Line2D):
        x, y = child.get_data()
        y *= n
        child.set_data(x,y)

# update y-limits (not done automatically)
ax.set_ylim(y.min(), y.max())
fig.canvas.draw()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2018-10-10
    • 2020-08-11
    • 2020-09-08
    • 2014-10-15
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多