首页 > 解决方案 > 无法在 python matplotlib 中设置图形 gid

问题描述

我想在 matplotlib 中设置图形 gid 并将其保存到 svg 文件中。我发现我可以通过调用得到正确的结果,get_gid()但无法进入 svg 文件。

我使用的测试代码是这样的:

import matplotlib.pyplot as plt

FIGURE_ID = "the_figure"
FIGURE_PATCH_ID = "the_figure_patch"

fig = plt.figure(figsize=(15, 9))
fig.clf()
fig.set_gid(FIGURE_ID)
fig.patch.set_gid(FIGURE_PATCH_ID)

print(fig.get_gid())
print(fig.patch.get_gid())

fig.savefig('id_test.svg', format='svg')

控制台的输出是:

the_figure
the_figure_patch

但是当我打开 svg 文件时,它是这样的:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (https://matplotlib.org/) -->
<svg height="648pt" version="1.1" viewBox="0 0 1080 648" width="1080pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs>
  <style type="text/css">
*{stroke-linecap:butt;stroke-linejoin:round;}
  </style>
 </defs>
 <g id="figure_1">
  <g id="the_figure_patch">
   <path d="M 0 648 
L 1080 648 
L 1080 0 
L 0 0 
z
" style="fill:#ffffff;"/>
  </g>
 </g>
</svg>

这证明补丁 gid 已更改,但图形 gid 仍然存在。请帮我弄清楚我的幼稚问题!先感谢您!

标签: pythonmatplotlibsvg

解决方案


推荐阅读