将SVG文件导入Matplotlib图 [英] Importing an svg file into a matplotlib figure
问题描述
我喜欢制作高质量的绘图,因此尽可能避免使用光栅化的图形.
I like to produce high quality plots and therefore avoid rasterized graphics as much as possible.
我正在尝试将svg文件导入到matplotlib图形上:
I am trying to import an svg file on to a matplotlib figure:
import matplotlib.pyplot as plt
earth = plt.imread('./gfx/earth.svg')
fig, ax = plt.subplots()
im = ax.imshow(earth)
plt.show()
这与png完美搭配.有人可以告诉我如何使用svg进行操作,或者至少将我指向适当的文档.
This works with png perfectly. Can somebody tell me how to do it with svg or at least point my to proper documentation.
I know that a similar question has been asked (but not answered): here. Has anything changed since?
P.S.我知道我可以导出高分辨率png并达到类似的效果.这不是我想要的解决方案.
P.S. I know that I could just export a high resolution png and achieve a similar effect. This is not the solution I am looking for.
这是我要导入的图像:
.
推荐答案
也许您正在寻找的是 svgutils
import svgutils.compose as sc
from IPython.display import SVG # /!\ note the 'SVG' function also in svgutils.compose
import numpy as np
# drawing a random figure on top of your SVG
fig, ax = plt.subplots(1, figsize=(4,4))
ax.plot(np.sin(np.linspace(0,2.*np.pi)), np.cos(np.linspace(0,2.*np.pi)), 'k--', lw=2.)
ax.plot(np.random.randn(20)*.3, np.random.randn(20)*.3, 'ro', label='random sampling')
ax.legend()
ax2 = plt.axes([.2, .2, .2, .2])
ax2.bar([0,1], [70,30])
plt.xticks([0.5,1.5], ['water ', ' ground'])
plt.yticks([0,50])
plt.title('ratio (%)')
fig.savefig('cover.svg', transparent=True)
# here starts the assembling using svgutils
sc.Figure("8cm", "8cm",
sc.Panel(sc.SVG("./Worldmap_northern.svg").scale(0.405).move(36,29)),
sc.Panel(sc.SVG("cover.svg"))
).save("compose.svg")
SVG('compose.svg')
输出:
这篇关于将SVG文件导入Matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!