【问题标题】:Automatically increase width of plot with the number of subplots in matplotlib根据 matplotlib 中子图的数量自动增加图的宽度
【发布时间】:2021-05-16 04:33:03
【问题描述】:

我想知道如何生成宽度随子图数量增加的图形。

我正在尝试创建一个包含多个箱线图的图表:

def create_box_plot(vals_dict, max_val):
    fig = plt.figure()
    ax = plt.axes()
    
    # set axes limits and labels
    plt.ylim(0, max_val)
    plt.xticks(rotation=90)
    
    counter, positionss = 1, []
    # first boxplot pair
    for vals in vals_dict.values():
        positions = [i for i in range(counter, counter + len(vals))]
        positionss.append(positions)
        bp = plt.boxplot(vals, positions=positions, widths=0.6, showfliers=False)
        
        counter += len(vals) + 1
    
    ax.set_xticklabels(list(vals_dict.keys()))
    ax.set_xticks([np.mean(positions) for positions in positionss])
    
    buf = io.BytesIO()
    
    plt.savefig(buf, aspect='auto', dpi=300, format='png')
    buf.seek(0)
    plt.close() 

如果我例如将figzise 设置为(20, 1),然后对于大量子图,我得到了一个足够好看的图表:

但是如果我只有几个箱线图或者标签很长,那么图表看起来很糟糕:

【问题讨论】:

    标签: python matplotlib plot graph data-visualization


    【解决方案1】:

    我认为您的意思不是子情节,而是情节中的框,对吗?

    figsize 参数传递给 plt.figure 并使 figsize 值取决于您要绘制的框数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      相关资源
      最近更新 更多