簡體   English   中英

Matplotlib-繪圖大小,如何調整大小?

[英]Matplotlib - Plot size, how to adjust the size?

我正在嘗試從我創建的繪圖中刪除空白區域,請參見下圖:

在此處輸入圖片說明

可以看到,右側和底部都有一個大白點,該如何解決?請按照我的腳本執行:

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)

任何幫助將不勝感激

您正在創建太多子圖!

如果我們看這一行:

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

我們可以看到給subplot2grid的第一個參數是要制作的子圖網格的尺寸,在本例中為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()

給予:

在此處輸入圖片說明

您可以使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM