【发布时间】:2021-01-09 22:09:18
【问题描述】:
我想将 matplotlib 折线图保存为长宽比为 3:1 且没有轴或标签的透明 png 图像。我需要图形的线条直接在图像的边缘开始和结束(没有任何填充)。
我发现了几个类似的主题,例如。 G。 tight savefig without axes in matplotlib 或 Removing white space around a saved image in matplotlib,但是这两个建议都没有帮助。
这是我的代码:
import matplotlib.pyplot as plt
x = np.arange(1, 10)
y = np.arange(51, 60)
plt.gca().set_axis_off()
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
fig = plt.figure(figsize=(9,3))
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
ax.set_axis_off()
ax.plot(x, y)
# plt.savefig("result.png", format="png", transparent=True, `bbox_inches="tight", pad_inches=0) # Result image is empty.
plt.savefig("result.png", format="png", transparent=True)
plt.show()
仍然,结果图像中有一些填充(有白色背景显示填充,但实际上图像是透明的):
有没有什么方法可以实现无填充的图表?
【问题讨论】:
标签: python matplotlib figure