Matplotlib savefig 图像修剪 [英] Matplotlib savefig image trim

查看:34
本文介绍了Matplotlib savefig 图像修剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例代码将生成一个没有轴的基本线图并将其保存为 SVG 文件:

The following sample code will produce a basic line plot with no axes and save it as an SVG file:

import matplotlib.pyplot as plt
plt.axis('off')
plt.plot([1,3,1,2,3])
plt.plot([3,1,1,2,1])
plt.savefig("out.svg", transparent = True)

如何设置图像的分辨率/尺寸?在折线图之外,图像的所有侧面都有填充.如何删除填充以使线条出现在图像的边缘?

How do I set the resolution / dimensions of the image? There is padding on all sides of the image beyond the line graph. How do I remove the padding so that the lines appear on the edge of the image?

推荐答案

我一直惊讶于在 matplotlib 中有多少种方法可以做同样的事情.
因此,我确信有人可以使此代码更简洁.
无论如何,这应该清楚地展示如何着手解决您的问题.

I am continually amazed at how many ways there are to do the same thing in matplotlib.
As such, I am sure that someone can make this code much more terse.
At any rate, this should clearly demonstrate how to go about solving your problem.

>>> import pylab
>>> fig = pylab.figure()

>>> pylab.axis('off')
(0.0, 1.0, 0.0, 1.0)
>>> pylab.plot([1,3,1,2,3])
[<matplotlib.lines.Line2D object at 0x37d8cd0>]
>>> pylab.plot([3,1,1,2,1])
[<matplotlib.lines.Line2D object at 0x37d8d10>]

>>> fig.get_size_inches()    # check default size (width, height)
array([ 8.,  6.])
>>> fig.set_size_inches(4,3) 
>>> fig.get_dpi()            # check default dpi (in inches)
80
>>> fig.set_dpi(40)

# using bbox_inches='tight' and pad_inches=0 
# I managed to remove most of the padding; 
# but a small amount still persists
>>> fig.savefig('out.svg', transparent=True, bbox_inches='tight', pad_inches=0)

文档 savefig()代码>.

这篇关于Matplotlib savefig 图像修剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆