【问题标题】:Different shading under Seaborn DistplotSeaborn Distplot 下的不同阴影
【发布时间】:2020-11-18 17:21:19
【问题描述】:

我正在尝试使用基于此 MIC(1) 线的阴影创建绘图。 上面和下面的阴影不同。

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

def createSkewDist(mean, sd, skew, size):

    # calculate the degrees of freedom 1 required to obtain the specific skewness statistic, derived from simulations
    loglog_slope=-2.211897875506251 
    loglog_intercept=1.002555437670879 
    df2=500
    df1 = 10**(loglog_slope*np.log10(abs(skew)) + loglog_intercept)

    # sample from F distribution
    fsample = np.sort(stats.f(df1, df2).rvs(size=size))

    # adjust the variance by scaling the distance from each point to the distribution mean by a constant, derived from simulations
    k1_slope = 0.5670830069364579
    k1_intercept = -0.09239985798819927
    k2_slope = 0.5823114978219056
    k2_intercept = -0.11748300123471256

    scaling_slope = abs(skew)*k1_slope + k1_intercept
    scaling_intercept = abs(skew)*k2_slope + k2_intercept

    scale_factor = (sd - scaling_intercept)/scaling_slope    
    new_dist = (fsample - np.mean(fsample))*scale_factor + fsample

    # flip the distribution if specified skew is negative
    if skew < 0:
        new_dist = np.mean(new_dist) - new_dist

    # adjust the distribution mean to the specified value
    final_dist = new_dist + (mean - np.mean(new_dist))

    return final_dist



desired_mean = 30
desired_skew = 1.5
desired_sd = 20

final_dist = createSkewDist(mean=desired_mean, sd=desired_sd, skew=desired_skew, size=1000000)

# inspect the plots & moments, try random sample
fig, ax = plt.subplots(figsize=(12,7))
sns.distplot(final_dist, 
             hist=False, 
             ax=ax, 
             color='darkred', 
             kde_kws=dict(linewidth=4))

l1 = ax.lines[0]


# Get the xy data from the lines so that we can shade
x1 = l1.get_xydata()[:,0]
x1[0] = 0

y1 = l1.get_xydata()[:,1]
y1[0] = 0

ax.fill_between(x1,y1, color="lemonchiffon", alpha=0.3)


ax.set_ylim(0.0001,0.03)
ax.axhline(0.002, ls="--")
ax.set_xlim(1.5, 200)
ax.set_yticklabels([])
ax.set_xticklabels([])


trans = transforms.blended_transform_factory(
    ax.get_yticklabels()[0].get_transform(), ax.transData)

ax.text(0,0.0025, "{}".format("MIC(1) = 1"), color="blue", transform=trans, 
        ha="right", va="top", fontsize = 12)



trans_2 = transforms.blended_transform_factory(
    ax.get_xticklabels()[0].get_transform(), ax.transData)

ax.text(84,0, "{}".format("\n84"), color="darkred", transform=trans_2, 
        ha="center", va="top", fontsize = 12)

ax.text(1.5,0, "{}".format("\n0"), color="darkred", transform=trans_2, 
        ha="center", va="top", fontsize = 12)


ax.axvline(x = 84, ymin = 0, ymax = 0.03, ls = '--', color = 'darkred' )


ax.set_yticks([])
ax.set_xticks([])

ax.spines['top'].set_color(None)
ax.spines['right'].set_color(None)
ax.spines['left'].set_linewidth(2)
ax.spines['bottom'].set_linewidth(2)
ax.set_ylabel("Concentration [mg/L]", labelpad = 80, fontsize = 15)
ax.set_xlabel("Time [h]", labelpad = 80, fontsize = 15)
ax.set_title("AUC/MIC", fontsize = 20, pad = 30)


plt.annotate("AUC/MIC", 
             xy=(18, 0.02), 
             xytext=(18, 0.03), 
             arrowprops=dict(arrowstyle="->"), fontsize = 12);

;

这就是我所拥有的:

这就是我想要的(它是在油漆中完成的,所以请原谅我 :)):

我正在尝试使用 fill_between 和 fill_betweenx。然而,没有任何令人满意的结果。确实,想法用完了。我真的很感激这方面的任何帮助。祝你好运!

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    您的fill_between 按预期工作。问题是 color="lemonchiffon" 和 alpha=0.3 几乎不可见。尝试为alpha 使用更亮的颜色和/或更高的值。

    因此,这会将图形的零和 kde 曲线之间的部分着色。

    现在,要在水平线上方和下方创建不同的颜色,where= 和 np.minimum 可以在 fill_between 中使用:

    pos_hline = 0.002
    ax.fill_between(x1, pos_hline, y1, color="yellow", alpha=0.3, where=y1 > pos_hline)
    ax.fill_between(x1, 0, np.minimum(y1, pos_hline), color="blue", alpha=0.3)
    

    如果没有where=y1 &gt; pos_hline,fill_between 也会为曲线上方的区域着色,其中曲线低于该水平线。

    PS:请注意,自 Seaborn 版本 0.11 起,sns.histplot 已被弃用。只绘制kde曲线,可以使用sns.kdeplot:

    sns.kdeplot(final_dist, ax=ax, color='darkred', linewidth=4)
    

    【讨论】:

    • 是的。这很棒。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 2020-02-10
    • 2016-08-05
    • 2016-09-01
    • 2020-10-20
    • 2019-08-12
    • 2015-10-20
    • 2019-08-03
    相关资源
    最近更新 更多