Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

python - plot SVG within matplotlib figure (embedded in wxPython)

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

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)

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

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

raise IOError("cannot identify image file")

IOError: cannot identify image file

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

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...