Matplotlib-打印大小,如何调整大小?

2024-06-01 01:56:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

可以看到,右边和底部都有一个大白点,怎么解决?,请按我的脚本执行:

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)

任何帮助都将不胜感激


Tags: selfdatatopfigpltaxtescolor
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)

你创建的子块太多了!

如果我们看这一行:

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

我们可以看到给subblot2grid的第一个参数是要生成的子块网格的维度,在本例中是4行3列。然后在图左上角的子图中绘制(给出第二个参数),这会留下很多未使用的空间。

所以要解决这个问题,可以使用以下方法减少子块的数量:

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

完整示例:

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

给予:

enter image description here

相关问题 更多 >