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

Raf*_*tos 0 python matplotlib

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

在此处输入图片说明

可以看到,右侧和底部都有一个大白点,该如何解决?请按照我的脚本执行:

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)
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

Dav*_*idG 5

您正在创建太多子图!

如果我们看这一行:

ax1 = plt.subplot2grid((4,3), (0,0),)
Run Code Online (Sandbox Code Playgroud)

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

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

ax1 = plt.subplot2grid((2,2), (0,0),)
Run Code Online (Sandbox Code Playgroud)

完整示例:

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()
Run Code Online (Sandbox Code Playgroud)

给予:

在此处输入图片说明