當我保存一個 matplotlib 圖時,我在包含我的圖的框周圍有不等的空白,因為左側有軸標記(底部同上)。
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1, 32)
plt.plot(x, x**2)
plt.show()

有沒有辦法可以讓影像中的框居中?
uj5u.com熱心網友回復:
您可以提取當前的左、右、下、上邊距,然后將它們設定為兩個方向的最大值。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1, 32)
plt.plot(x, x ** 2)
fig = plt.gcf()
left = max(fig.subplotpars.left, 1 - fig.subplotpars.right)
bottom = max(fig.subplotpars.bottom, 1 - fig.subplotpars.top)
fig.subplots_adjust(left=left, right=1 - left, bottom=bottom, top=1 - bottom)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371230.html
標籤:Python matplotlib
下一篇:無法創建箱線圖可能是尺寸問題
