python - IPython.显示 : SVG cannot render a svg image generated by svg_filter_line (official example)

标签 python matplotlib svg jupyter-notebook

我正在运行 svg_filter_line (官方示例)在带有 Python3 的 MacOS 上的 Jupyter 中。

SVG 适用于大量 svg 图像(例如来自 wiki 的世界地图图像)

enter image description here

svg_filter_line 的原始编码也能很好地工作。

enter image description here

运行 svg_filter_line 会生成一个名为“svg_filter_line.svg”的 svg 图像。

SVG 无法呈现此 svg 图像“svg_filter_line.svg”,无错误,无警告。

enter image description here

最佳答案

要做到这一点有点困难。这是解决方案:在处理文件时:我发现 display()display_svg() 函数显示的文件类型彼此略有不同(差异主要在命名空间中)。作为解决方法,必须使用 svgutils 重新保存 SVG 文件,然后读取并显示如下:

import svgutils.transform as sg
from IPython.display import SVG,display

#create new SVG figure
fig = sg.SVGFigure("16cm", "10cm")

# load matpotlib-generated figures
fig1 = sg.fromfile('svg_filter_line.svg')
plot1 = fig1.getroot()
fig.append([plot1])
fig.save("svg_filter_line2.svg")
display(SVG(filename='svg_filter_line2.svg'))

svgutils 可以安装如下:

conda install -c conda-forge svgutils

关于python - IPython.显示 : SVG cannot render a svg image generated by svg_filter_line (official example),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56514491/

相关文章:

python - 使用 MariaDB 10.2 安装 mysqlclient 时出错

python - 谷歌 BigQuery 安装

python - python seaborn 图中的图例标签不正确

python - wxpython-plotting 中的 Matplotlib 中止

javascript - 使用 remove() 删除元素,但其属性仍然存在

text - 带有弯曲文本路径的 SVG 文本对齐问题

python for 循环列表加一项

python - 用 Python Pandas 写入 to_csv : Choose which column index to insert new data

python - Matplotlib - 不同时刻特定轮廓的图例

internet-explorer - 对象不支持此属性或方法