Matplotlib savefig 图像修剪

发布于 2024-09-06 21:32:18 字数 259 浏览 3 评论 0原文

以下示例代码将生成一个没有轴的基本线图并将其保存为 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)

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖正好 2024-09-13 21:32:18

我一直对 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)

savefig() 的文档

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)

Documentation for savefig().

从﹋此江山别 2024-09-13 21:32:18

默认的轴对象为标题、刻度标签等留出了一些空间。制作自己的轴对象来填充整个区域:

fig=figure()
ax=fig.add_axes((0,0,1,1))
ax.set_axis_off()
ax.plot([3,1,1,2,1])
ax.plot([1,3,1,2,3])
fig.savefig('out.svg')

在 svg 格式中,我看不到底部的线条,但在 png 格式中我可以,所以这可能是 svg 渲染器的一个功能。您可能只想添加一点填充以使所有内容都可见。

The default axis object leaves some room for titles, tick labels and the like. Make your own axis object that fills the whole area:

fig=figure()
ax=fig.add_axes((0,0,1,1))
ax.set_axis_off()
ax.plot([3,1,1,2,1])
ax.plot([1,3,1,2,3])
fig.savefig('out.svg')

In svg format I can't see the line that's right at the bottom, but in png format I can, so it's probably a feature of the svg renderer. You might want to add just a little padding to keep everything visible.

何以畏孤独 2024-09-13 21:32:18

减少大部分填充的一个非常简单的方法是在保存图形之前调用tight_layout()。

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 200)

fig, ax = plt.subplots()
ax.plot(x, np.sin(x))

fig.tight_layout()
fig.savefig('plot.pdf')

A very easy way to trim down most padding is to call tight_layout() before saving the figure.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 200)

fig, ax = plt.subplots()
ax.plot(x, np.sin(x))

fig.tight_layout()
fig.savefig('plot.pdf')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文