Python Matplotlib - 绘图大小,如何调整大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48097995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:32:11  来源:igfitidea点击:

Matplotlib - Plot size, how to adjust the size?

pythonmatplotlib

提问by Rafael Rodrigues Santos

I'm trying to remove the white space from the plot that I created,please see the photo below:

我正在尝试从我创建的图中删除空白区域,请参见下面的照片:

enter image description here

在此处输入图片说明

As it is possible to see, there a big white spot on the right and also on the bottom, how to fix it?, follow down my script:

可以看到,右侧和底部都有一个大白点,如何修复它?,请按照我的脚本进行操作:

fig = plt.figure(figsize=(7,7))


ax1 = plt.subplot2grid((4,3), (0,0),)
ax2 = plt.subplot2grid((4,3), (1,0),)
ax3 = plt.subplot2grid((4,3), (0,1),)
ax4 = plt.subplot2grid((4,3), (1,1),)

data = self.dframe[i]

tes = print_data(data, self.issues, self.color, self.type_user)

tes.print_top(data=data, top=10, ax=ax1, typegraph="hbar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax2, typegraph="prod_bar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax3, typegraph="reg_hbar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax4, typegraph=self.type_user, problem=self.issues[i], tone=self.color[i])

problem = self.issues[i]
plt.tight_layout()
name = problem + str('.PNG')
plt.close(fig)
fig.savefig(name)

Any help will be highly appreciated

任何帮助将不胜感激

回答by DavidG

You are creating too many subplots!

您正在创建太多子图!

If we look at this line:

如果我们看这一行:

ax1 = plt.subplot2grid((4,3), (0,0),)

We can see the first argument given to subplot2grid are the dimensions of the subplot grid to be made, in this case 4 rows, and 3 columns. You are then plotting in the subplots in the top left of your figure (the second argument given) which leaves a lot of space that's not used.

我们可以看到给 subplot2grid 的第一个参数是要制作的子图网格的尺寸,在本例中为 4 行和 3 列。然后,您将在图形左上角的子图中进行绘图(给出的第二个参数),这留下了很多未使用的空间。

So to solve this, reduce the number of subplots by using:

因此,要解决此问题,请使用以下方法减少子图的数量:

ax1 = plt.subplot2grid((2,2), (0,0),)

Full example:

完整示例:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(25)

fig = plt.figure(figsize=(7,7))

ax1 = plt.subplot2grid((2,2), (0,0),)
ax2 = plt.subplot2grid((2,2), (1,0),)
ax3 = plt.subplot2grid((2,2), (0,1),)
ax4 = plt.subplot2grid((2,2), (1,1),)

ax1.plot(data)
ax2.plot(data)
ax3.plot(data)
ax4.plot(data)

plt.show()

Giving:

给予:

enter image description here

在此处输入图片说明

回答by ymmx

you can use plt.subplots_adjust(left=0.09, bottom=0.07, right=0.98, top=0.97, wspace=0.2 , hspace=0.17 )to adjust the window. But the issue is that a lot of the space in your plot is empty maybe you should change plt.subplot2grid((4,3)... to plt.subplot2grid((2,2)

可以 plt.subplots_adjust(left=0.09, bottom=0.07, right=0.98, top=0.97, wspace=0.2 , hspace=0.17 )用来调整窗口。但问题是你的情节中的很多空间都是空的,也许你应该改变 plt.subplot2grid((4,3)......plt.subplot2grid((2,2)