【发布时间】: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