簡體   English   中英

Seaborn Distplot 下的不同陰影

[英]Different shading under Seaborn Distplot

我正在嘗試使用基於此 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。 然而,沒有任何令人滿意的結果。 當然,沒有想法了。 我真的很感激這方面的任何幫助。 最好的祝願!

您的fill_between按預期工作。 問題是alpha=0.3 color="lemonchiffon"幾乎不可見。 嘗試使用更亮的顏色和/或更高的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 > pos_hlinefill_between也會為曲線上方的區域着色,其中曲線低於該水平線。

結果圖

PS:請注意,自 Seaborn 版本0.11以來, sns.histplot已被棄用。 要僅繪制 kde 曲線,您可以使用sns.kdeplot

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM