python - 减少matplotlib图中的左右边距

我正在努力处理我在 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/

相关文章:

c - 如何在 C 中与 Linux 一起使用共享内存

python - 将列表列表获取到 pandas DataFrame

python - 如何在 Python 中实现树?

python - 重命名 virtualenv 文件夹而不破坏它

linux - 如何在 Linux shell 脚本中插入新行?

linux - 如何将当前正在运行的 linux 进程置于后台?

python - 拆分(分解) Pandas 数据框字符串条目以分隔行

python - _tkinter.TclError : no display name and n

linux - 用于计算已用时间的 Bash 脚本

linux - 如何找到我使用的 Fedora 版本?