如何在海底分布图中填充曲线下的区域 [英] How to fill area under curve in seaborn distribution plot
问题描述
我有两个变量
x = [1.883830, 7.692308,8.791209, 9.262166]
y = [5.337520, 4.866562, 2.825746, 6.122449]
我想使用为 matplotlib 包装的 seaborn 拟合高斯分布.看起来 sns.distplot
函数是最好的方法,但我不知道如何填充曲线下的区域.帮助吗?
And I want to fit a Gaussian distribution using the seaborn wrapped for matplotlib. It seems like the sns.distplot
function is the best way to do this, but I can't figure out how to fill in the area under the curve. Help?
fig, ax = plt.subplots(1)
sns.distplot(x,kde_kws={"shade":True}, kde=False, fit=stats.gamma, hist=None, color="red", label="2016", fit_kws={'color':'red'});
sns.distplot(y,kde_kws={"shade":True}, kde=False, fit=stats.gamma, hist=None, color="blue", label="2017", fit_kws={'color':'blue'})
我认为"shade"参数可能是 fit_kws
参数的一部分,但是我还没有做到这一点.
I think the "shade" argument could be part of the fit_kws
argument but I haven't gotten this to work.
另一种选择是使用 ax.fill()
?
推荐答案
是的,与 kde_kws
不同,fit_kws
不支持 shade
参数代码>.但是正如您所猜到的,我们可以使用 ax.fill_between()
填充两条曲线下方的区域.我们将不得不从 ax
对象及其x-y数据中获取直线,然后使用该数据填充曲线下的区域.这是一个例子.
Yes, the shade
argument is not supported for fit_kws
unlike for the kde_kws
. But as you guessed, we can fill the area under the two curves using ax.fill_between()
. We will have to get the lines from the ax
object and their x-y data and then use that to fill the area under the curves. Here is an example.
import numpy as np
import seaborn as sns
import scipy.stats as stats
import matplotlib.pyplot as plt
x = [1.883830, 7.692308,8.791209, 9.262166]
y = [5.337520, 4.866562, 2.825746, 6.122449]
ax = sns.distplot(x, fit_kws={"color":"red"}, kde=False,
fit=stats.gamma, hist=None, label="label 1");
ax = sns.distplot(y, fit_kws={"color":"blue"}, kde=False,
fit=stats.gamma, hist=None, label="label 2");
# Get the two lines from the axes to generate shading
l1 = ax.lines[0]
l2 = ax.lines[1]
# Get the xy data from the lines so that we can shade
x1 = l1.get_xydata()[:,0]
y1 = l1.get_xydata()[:,1]
x2 = l2.get_xydata()[:,0]
y2 = l2.get_xydata()[:,1]
ax.fill_between(x1,y1, color="red", alpha=0.3)
ax.fill_between(x2,y2, color="blue", alpha=0.3)
plt.show(block=False)
结果如下图:
这篇关于如何在海底分布图中填充曲线下的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!