【问题标题】:Plotting multiple horizontal lines for each distribution in strip plot subplots Matplotlib在带状图子图中为每个分布绘制多条水平线 Matplotlib
【发布时间】:2019-04-07 23:53:36
【问题描述】:

我正在尝试将平均计算值绘制为一条穿过我的数据集的每个绘制分布中心的线。

我的代码如下所示:

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 轴类别) 水平线仅出现 一次

我怎样才能得到它,以便每组编号的分类值都有各自的平均值?

【问题讨论】:

  • 不幸的是没有。
  • 我应该在您提供答案时立即跟进。这是我的错——我很抱歉。

标签: python python-3.x matplotlib plot


【解决方案1】:

由于您没有提供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)

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2018-10-29
    • 1970-01-01
    • 2017-05-09
    • 2017-03-29
    • 1970-01-01
    • 2013-02-18
    • 2018-05-29
    • 2013-04-29
    相关资源
    最近更新 更多