簡體   English   中英

在條帶圖子圖中使用Matplotlib為每個分布繪制多條水平線

[英]Plotting multiple horizontal lines for each distribution in strip plot subplots Matplotlib

我試圖將平均計算值繪制為通過我的數據集的每個繪制分布的中心的線。

我的代碼看起來像這樣:

for plot, var in zip(range(1, plot_num+1), var_list):

    ax = fig.add_subplot(2, 2, plot)

    # calculate averages

    sns.stripplot(x=cluster_index_sample[cluster_type], y=cluster_index_sample[var], 
                  jitter=jitter, linewidth=line_width, alpha=alpha, cmap=RS_colorwheel,
                  size=marker_size, ax=ax)

    # create average lines
    ax.axhline(y=cluster_index_sample['Average_'+(var)].iloc[0], 
                                      linewidth=3, xmin=0.2, xmax=0.5)

    ax.set_ylabel(str(var), fontsize=y_lab)
    ax.set_xlabel('')
    ax.tick_params(axis='both', which='major', pad=10)

但是當我繪制這個時,每個cluster_type(x軸類別)只出現一次水平線。

在此輸入圖像描述

我怎樣才能得到它,以便每組編號的分類值得到各自的平均值?

由於您沒有提供MCVE ,我無法運行您的代碼。 不過,您可以嘗試使用第二個for循環迭代所有變量以繪制水平平均線,如下所示。 您還必須修改每行的xminxmax 我把它留給你。

for plot, var in zip(range(1, plot_num+1), var_list):
    ax = fig.add_subplot(2, 2, plot)
    sns.stripplot(x=cluster_index_sample[cluster_type], y=cluster_index_sample[var], 
                  jitter=jitter, linewidth=line_width, alpha=alpha, cmap=RS_colorwheel,
                  size=marker_size, ax=ax)
    for v in var_list: # <--- Added here
        ax.axhline(y=cluster_index_sample['Average_'+(v)].iloc[0], 
                                      linewidth=3, xmin=0.2, xmax=0.5) # <--- Added here

    ax.set_ylabel(str(var), fontsize=y_lab)
    ax.set_xlabel('')
    ax.tick_params(axis='both', which='major', pad=10)

暫無
暫無

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

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