【问题标题】:tight savefig without axes in matplotlibmatplotlib 中没有轴的紧凑 savefig
【发布时间】:2020-08-25 23:33:38
【问题描述】:

当使用 matplotlib 绘制没有轴的东西时,savefig() 并不是真正的“紧密”:

import matplotlib.pyplot as plt

circ = plt.Circle((0, 0), 1.0)
plt.gca().add_artist(circ)
plt.gca().set_aspect("equal")
plt.axis("off")
# plt.show()
plt.savefig("out.svg", bbox_inches="tight")

那是因为 SVG 包含隐藏的“背景补丁”

  <g id="patch_1">
   <path d="M 0 280.512 
L 280.512 280.512 
L 280.512 0 
L 0 0 
z
" style="fill:none;"/>
  </g>

如何去除?

【问题讨论】:

  • 你能试试plt.savefig("out.svg", bbox_inches="tight", transparent=True)吗?
  • @Sheldore 也不起作用。
  • 也许在保存之前也设置plt.gcf().patch.set_visible(False)

标签: python matplotlib


【解决方案1】:

pad_inches 选项!

plt.savefig("out.svg", bbox_inches="tight", pad_inches=0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 2022-01-01
    • 2013-11-15
    • 2018-01-21
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多