python 绘制图表生成svg文件_python – 如何将matplotlib图的输出作为SVG?

我需要获取一个matplotlib图的输出,并将其转换成可以在激光切割器上使用的SVG路径。

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等程序中使用。

我知道一个名为“Cairo”的SVG库,它可以使用matplotlib(matplotlib.use(‘Cairo’)),但是我并不清楚,这将使我能够访问我需要的SVG路径,即使matplotlib将现在正在使用开罗来生成情节。

我确实有开罗工作在我的系统,并可以成功地绘制一个由SVG路径组成的示例,我可以在Illustrator中编辑,但是我没有办法把我的方程式放到SVG路径中。

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图形显示)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值