python - 如何消除 matplotlib 保存的图形中的边距?

标签 python matplotlib

我有一个由矩阵 x 的 matplotlib 保存为 .png 文件的图形:

fig = plt.figure(figsize=(1,1), dpi=100)
plt.imshow(x)
fig.savefig('filename.png')

这是在预览中打开的结果:

enter image description here

有一个我不需要的白边,我的问题是:

  • 如何在 python 中保存没有此边距的图像?

P.S.:如果我尝试其他帖子中找到的不同解决方案,我不会得到我需要的结果。例如解决方案:

fig.savefig('filename.png', bbox_inches='tight')

fig.savefig('filename.png', bbox_inches='tight', pad_inches=0)

enter image description here

最佳答案

savefig中使用bbox_inches='tight':

fig.savefig('filename.png', bbox_inches='tight')

关于python - 如何消除 matplotlib 保存的图形中的边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52931148/

相关文章:

python - 从 np.datetime64 转换为浮点年份

python - 使自定义记录器可跨多个模块使用

python - 绘制 Pandas Dataframe 的直方图及其均值和标准差,得到 ValueError

python - 如何使用 matplotlib 在线图上叠加散点图?

python - 极轴不显示数据(matplotlib)

python - 安装 python mptt 权限被拒绝

python - python 函数返回生成器或普通对象的问题

python - 使用 Python 访问嵌套的 JSON 值

python - 当子图共享 x 轴时如何停止 xlim 更新

python - 删除 pandas plot.area 的抗锯齿