如何获得matplotlib图的输出作为SVG? [英] How can I get the output of a matplotlib plot as an SVG?

查看:1663
本文介绍了如何获得matplotlib图的输出作为SVG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取matplotlib图的输出,并将其转换为可以在激光切割机上使用的SVG路径.

I need to take the output of a matplotlib plot and turn it into an SVG path that I can use on a laser cutter.

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,100,0.00001)
y = x*np.sin(2*pi*x)
plt.plot(y)
plt.show()

例如,在下面您看到一个波形.我希望能够将该波形输出或保存为SVG路径,以后可以在诸如Adobe Illustrator之类的程序中使用.

For example, below you see a waveform. I would like to be able to output or save this waveform as an SVG path that I can later work with in a program such as Adobe Illustrator.

我知道matplotlib可以使用的名为"Cairo"的SVG库(matplotlib.use('Cairo')),但是我不清楚这是否可以访问所需的SVG路径,即使现在可以使用matplotlib用开罗生成图.

I am aware of an SVG library called "Cairo" that matplotlib can use (matplotlib.use('Cairo')), however it's not clear to me that this will give me access to the SVG path that I need, even though matplotlib will now be using Cairo to generate the plot.

我的系统上确实有cairo,并且可以成功地绘制一个由SVG路径组成的示例,而我确实可以在Illustrator中对其进行编辑,但是我无法将上面的等式带入SVG路径中.

I do have cairo working on my system, and can successfully draw an example composed of SVG paths that I can indeed edit in Illustrator, but I don't have a way to take my equation above into an SVG path.

import cairo
from cairo import SVGSurface, Context, Matrix    
s = SVGSurface('example1.svg', WIDTH, HEIGHT)
c = Context(s)

# Transform to normal cartesian coordinate system
m = Matrix(yy=-1, y0=HEIGHT)
c.transform(m)

# Set a background color
c.save()
c.set_source_rgb(0.3, 0.3, 1.0)
c.paint()
c.restore()

# Draw some lines
c.move_to(0, 0)
c.line_to(2 * 72, 2* 72)
c.line_to(3 * 72, 1 * 72)
c.line_to(4 * 72, 2 * 72)
c.line_to(6 * 72, 0)
c.close_path()
c.save()
c.set_line_width(6.0)
c.stroke_preserve()
c.set_source_rgb(0.3, 0.3, 0.3)
c.fill()
c.restore()

# Draw a circle
c.save()
c.set_line_width(6.0)
c.arc(1 * 72, 3 * 72, 0.5 * 72, 0, 2 * pi)
c.stroke_preserve()
c.set_source_rgb(1.0, 1.0, 0)
c.fill()
c.restore()

# Save as a SVG and PNG
s.write_to_png('example1.png')
s.finish()

(请注意,此处显示的图像是png,因为stackoverflow不接受svg图形显示)

(note that the image displayed here is a png, as stackoverflow doesn't accept svg graphics for display)

推荐答案

您很可能希望固定图像大小并摆脱各种背景和轴标记:

You will most probably want to fix the image size and get rid of all sorts of backgrounds and axis markers:

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=[6, 6])
x = np.arange(0, 100, 0.00001)
y = x*np.sin(2* np.pi * x)
plt.plot(y)
plt.axis('off')
plt.gca().set_position([0, 0, 1, 1])
plt.savefig("test.svg")

生成的SVG文件仅包含一个额外的元素,因为savefig确实要保存图形背景.此背景的颜色很容易更改为无",但似乎并不能消除它.无论如何,SVG还是非常干净,并且比例正确(每单位1/72英寸).

The resulting SVG file contains only one extra element, as savefig really wants to save the figure background. The color of this background is easy to change to 'none', but it does not seem to get rid of it. Anyway, the SVG is very clean otherwise and in the correct scale (1/72" per unit).

这篇关于如何获得matplotlib图的输出作为SVG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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