我正在努力处理我在 matplotlib 中的绘图边距。我使用下面的代码来制作我的图表:
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
但是,我得到一个输出图,图的两边都有很多空白。我搜索了谷歌并阅读了 matplotlib 文档,但我似乎找不到如何减少这种情况。
最佳答案
自动执行此操作的一种方法是将 bbox_inches='tight'
kwarg 转换为 plt.savefig
.
例如
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(3000).reshape((100,30))
plt.imshow(data)
plt.savefig('test.png', bbox_inches='tight')
另一种方法是使用 fig.tight_layout()
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(0, 1, 20); ys = np.sin(xs)
fig = plt.figure()
axes = fig.add_subplot(1,1,1)
axes.plot(xs, ys)
# This should be called after all axes have been added
fig.tight_layout()
fig.savefig('test.png')
https://stackoverflow.com/questions/4042192/