如何使用 matplotlib subplot 垂直拉伸图形

How to vertically stretch graphs with matplotlib subplot

使用以下代码,我尝试使用 Matplotlib 在一张图片中绘制 12 个不同的直方图。

for graph in range(1,13):
    name = all_results[graph-1]
    plt.subplot(4,3,graph, 
                title = name)
    plt.tight_layout()
    current_model = name
    plt.hist(all_val_accuracies[current_model],
             #range = (0.49, 0.58),
             bins = 50)
    plt.xlabel('Accuracy')
    plt.ylabel('Frequency')
    plt.axis([0.48, 0.58, 0, 50])

使用此代码,所有直方图都按照我的指示绘制在一张图像中。

However, the graphs itself are squeezed to such an extend that you cant see them anymore

怎样才能把这12个直方图画在一张图上,而且每个直方图都看得清楚?

您应该可以使用

设置绘图的大小
matplotlib.Figure.set_size_inches

要使用它,您需要保存 Figure 对象。您可以像这样将其包含在您的代码中:

for graph in range(1,13):
    name = all_results[graph-1]
    fig, axs = plt.subplot(4,3,graph, 
                           title = name)
    plt.tight_layout()
    current_model = name
    plt.hist(all_val_accuracies[current_model],
             #range = (0.49, 0.58),
             bins = 50)
    fig.set_size_inches(12, 12)
    plt.xlabel('Accuracy')
    plt.ylabel('Frequency')
    plt.axis([0.48, 0.58, 0, 50])