简体   繁体   中英

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

I'm trying to plot the average calculated values as a line through the center of each plotted distribution for my data set.

My code looks like this:

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)

But when I plot this the horizontal lines only appear once per cluster_type (x-axis category) .

在此输入图像描述

How can I get it so that each set of numbered categorical values gets their own respective averages?

Since you did not provide a MCVE , I can't run your code. Nevertheless, you can try using a second for loop to iterate through all the variables for plotting the horizontal average line as follows. You will also have to modify the xmin and xmax for each line. I leave that up to you.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM