如何使用matplotlib子图垂直拉伸图

问题描述:

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

With the following code, I try to plot 12 different histograms in one picture using Matplotlib.

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])

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

With this code, all histograms are plotted in one image as how I indicated it.

但是,图形本身被挤压到了无法再看到它们的程度

我应该怎么做才能将这12个直方图绘制在一张图像中,并且可以清楚地看到每个直方图?

What can I do so that these 12 histograms are plotted in one image and that each histogram can be seen clearly?

您应该能够使用

matplotlib.Figure.set_size_inches

要使用此功能,您将需要保存Figure对象.您可以像这样在您的代码中添加它:

To use this you will need to save the Figure object. You can include this in your code like this :

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])