python - 将 svg 文件导入 matplotlib 图

标签 python matplotlib svg python-imaging-library

我喜欢制作高质量的绘图,因此尽可能避免使用光栅化图形。

我正在尝试将 svg 文件导入到 matplotlib 图:

import matplotlib.pyplot as plt
earth   = plt.imread('./gfx/earth.svg')
fig, ax = plt.subplots()
im      = ax.imshow(earth)
plt.show()

这与 png 完美配合。有人可以告诉我如何使用 svg 做到这一点,或者至少指出我的正确文档。

我知道有人问过类似的问题(但没有回答):here .从那以后有什么改变吗?

附言我知道我可以导出高分辨率的 png 并达到类似的效果。这不是我正在寻找的解决方案。

这是我要导入的图像:

Earth_from_above .

最佳答案

也许您要找的是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')

输出:

enter image description here

关于python - 将 svg 文件导入 matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31452451/

相关文章:

python - 如何在 Django Web 应用程序中生成可使用表单参数化的 Matplotlib 图

python - 使用 Gimp Python 插件导出所有 SVG 路径

python - 在 Matplotlib 中显示多条线图例

python - 在 Django 模型中访问用户

python - 如何模拟 ManyToMany 字段?

python - Django ORM查询连接两个字段上不相关的表

python - Pyplot - 根据 y 轴值显示 x 轴标签

javascript - 附加文本不会显示 d3.js

css - svg 元素的 'display' 属性如何影响鼠标事件?

python - 循环运行整个单元测试