在matplotlib图中绘制SVG(嵌入在wxPython中) [英] plot SVG within matplotlib figure (embedded in wxPython)

查看:214
本文介绍了在matplotlib图中绘制SVG(嵌入在wxPython中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将SVG图像嵌入到matplotlib子图中.

I am trying to embed a SVG image within a matplotlib subplot.

matplotlib只能本地读取PNG,但是如果安装了PIL,它将使用它来加载图像并返回可以与imshow()一起使用的数组(如果可能). http://matplotlib.sourceforge.net/api/pyplot_api.html

matplotlib can only read PNGs natively, but if PIL is installed, it will use it to load the image and return an array (if possible) which can be used with imshow(). http://matplotlib.sourceforge.net/api/pyplot_api.html

import Image     
filename = "/tmp/figure.svg"
pil_img = Image.open(filename) # PIL image
img = np.asarray(pil_img) # converting PIL image into numpy array
self.axes.cla()
self.axes.imshow(img)

但是,当然PIL不支持SVG.错误:

But of course PIL does not support SVG. The error:

文件"/usr/lib/python2.7/dist-packages/PIL/Image.py",行1980,在 打开

File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1980, in open

提高IOError(无法识别图像文件")

raise IOError("cannot identify image file")

IOError:无法识别图像文件

IOError: cannot identify image file

如何栅格化SVG文件以使用imshow进行绘制或直接使用matplotlib绘制矢量?

How can I rasterize the SVG file to plot using imshow or directly plot the vectors using matplotlib?

谢谢.

推荐答案

我也遇到了同样的问题,但是现在在我的程序包pylustrator中实现了一个解决方案: https://pylustrator.readthedocs.io/en/latest/composed. html#svg文件

I also had the same problem but now implemented a solution in my package pylustrator: https://pylustrator.readthedocs.io/en/latest/composing.html#svg-files

它可以读取和解析svg文件,并创建matplotlib艺术家以在图中显示svg内容.

It can read and parse svg files and creates matplotlib artists to display the svg content in the figure.

并非所有的svg功能都可以由matplotlib艺术家模拟,例如不会显示渐变或滤镜,但支持大多数svg功能.

Not all svg feature can be emulated by matplotlib artists, so e.g. gradients or filters will not be displayed, but the majority of svg features are supported.

这篇关于在matplotlib图中绘制SVG(嵌入在wxPython中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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