matplotlib: 如何设置生成的图片比例?

2014-04-09 10:04:35 +08:00
 Lucups
为了去除图片的边缘空白部分,我使用了

fig = plt.figure()
ax = fig.add_subplot(111)
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('test.png', dpi = 200, bbox_inches = extent)

然后默认生成的图片宽高比例都是 31:24 的。

测试了一下直接 show

plt.show()

show出来的图片宽高比例是1:1的。

该如何设置图片的比例?
12815 次点击
所在节点    Python
4 条回复
autozimu
2014-04-09 22:29:24 +08:00
figure(figsize=None, ...)

figsize : tuple of integers, optional, default : None
width, height in inches. If not provided, defaults to rc
figure.figsize.

为什么要去除空白部分? 使用 savefig 的 transparent=True 参数不是很好吗?
Lucups
2014-04-10 00:06:52 +08:00
@autozimu
非常感谢!
因为有坐标轴的原因,transparent=True 会把坐标轴包含在内。
我试了一下 figure(figsize=[n,n]) (n=16, dpi=100) 可以实现等比,本身没有问题,但在我的那个代码里,生成的图片还是有一点点小小的差距(1240*1280)。
能否不显示坐标轴?
Hualin
2014-04-10 02:26:03 +08:00
@Lucups plt.axis('off')
Lucups
2014-04-12 22:01:59 +08:00
@Hualin 我已经找到最优方案, 感谢回答~!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/107784

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX