读取 svg 文件以插入 matplotlib

问题描述

我正在尝试从 SVG 文件中读取图像并插入到 matplotlib 图中。

import matplotlib.pyplot as plt
import pylustrator as pyl
import numpy as np
from matplotlib.figure import figure
from matplotlib.offsetBox import Offsetimage,AnnotationBBox
from svglib.svglib import svg2rlg

ax = plt.subplot(111)
ax.plot(
    [1,2,3],[1,'go-',label='line 1',linewidth=2
 )
# arr_img = plt.imread("stinkbug.svg")
# arr_img = svg2rlg("stinkbug.svg")
arr_img = pyl.load("stinkbug.svg")
im = Offsetimage(arr_img)
ab = AnnotationBBox(im,(1,0),xycoords='axes fraction')
ax.add_artist(ab)
plt.show()

我尝试使用 pylustratorpost 来读取 svg 图像。当输入图像为 png 格式时,该代码有效。但我无法添加保存在 svg 扩展名 (image) 中的相同图像。

由于以下错误,我无法成功插入,

    "float".format(self._A.dtype))
TypeError: Image data of dtype object cannot be converted to float

有关如何解决此问题的建议将非常有用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)