简体   繁体   English

如何改变 Matplotlib 中子图的大小?

[英]How change sizes of subplots in Matplotlib?

I am using Google Colab and when I try to draw a graph it comes like this我正在使用 Google Colab,当我尝试绘制图形时,它是这样的在此处输入图片说明

But I have to plot many graphs, so i used a for loop但是我必须绘制很多图,所以我使用了 for 循环在此处输入图片说明

I expected the graphs to be the same size as in the first image but I got all graphs in compressed form.我希望图形的大小与第一张图像中的大小相同,但我以压缩形式获得了所有图形。 What should I do to obtain the graphs as the same size as in first image using a for loop.我应该怎么做才能使用 for 循环获得与第一张图像相同大小的图形。

尝试使用figsize参数:

fig, ax = plt.subplots(no_of_feature, 1, figsize=(5, 10))  # example

When you do:当你这样做时:

fig, ax = plt.subplot(no_of_features,1)

You create a single instance of figure, which has a predefined size.您创建一个图形实例,它具有预定义的大小。 thus it will squeeze all the subplots to fit this figure size.因此它将挤压所有子图以适应这个图形大小。

To increase the figure size you should run with the size the you wish before calling plt.subplot :要增加图形大小,您应该在调用plt.subplot之前使用您希望的大小运行:

plt.rcParams['figure.figsize'] = [12, 8]

Or you can create a new plot in each iteration in a different figure, which each image will have the original one size.或者您可以在不同图形的每次迭代中创建一个新图,每个图像都将具有原始大小。

As @RichieV suggested, it also advisable to use different nrows / ncols to spread the subplots in the image.正如@RichieV 建议的那样,还建议使用不同的nrows / ncols来扩展图像中的子图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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