[英]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循環迭代所有變量以繪制水平平均線,如下所示。 您還必須修改每行的xmin
和xmax
。 我把它留給你。
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.