Matplotlib savefig图像修剪

ode*_*den 30 python matplotlib

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

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)
Run Code Online (Sandbox Code Playgroud)

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

ber*_*nie 52

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

>>> 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)
Run Code Online (Sandbox Code Playgroud)

文档savefig().

  • 有没有办法把这些放在matplotlibrc?"坏键"savefig.bbox_inches"` (4认同)