我有一个从 SVG 文件创建并使用cairosvg库加载的图表。
我想通过直接 matplotlib 命令以编程方式定义的附加叠加图进一步增强此图。
我正在尝试在内存中执行此操作,而不是写出文件并重新加载。仅保存最终文件。
然而,事实证明很难将现有的 cairo 与 matplotlib 呈现的结合起来。我认为这是一个潜在的解决方案,但不确定。
简化示例:
import matplotlib
matplotlib.use('Cairo') # Make sure here that our figures will use Cairo!
from cairosvg import surface
from cairosvg import parser
import matplotlib.pyplot as plt
import matplotlib.image as img
import numpy as np
SVG='\
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="500" height="500">\
<rect id="arena" x="10" y="10" width="480" height="480" fill="#ff5555" fill-opacity="0.5"/>\
</svg>\
'
svg = parser.Tree(bytestring=SVG)
surf = surface.PNGSurface(svg,None,1)
#surf.cairo.write_to_png("test-svg.png") # Can do this to see svg render to file
surf.finish() # Required?
ctx = surf.context # Can get the context here fine..
fig = plt.imshow(ctx) # <----- MAGIC REQUIRED HERE ABOUTS
#fig = plt.figure() # This works like a normal plot - but no svg
ax = fig.gca() # Create some overlay data
x = np.array(np.random.randn(100))
y = np.array(np.random.randn(100))
ax.plot(x,y)
plt.savefig("test-final.png") # Save svg & overlay plot
编辑:也一直在尝试以下,基于前面的链接。不过还是不开心。
from matplotlib.artist import Artist
class SurfArtist(Artist):
def __init__(self,surf):
Artist.__init__(self)
self._surf = surf
def draw(self,renderer):
from matplotlib.backends.backend_cairo import RendererCairo
if not isinstance(renderer, RendererCairo):
raise TypeError("backend not supported")
ctx = renderer.gc.ctx if hasattr(renderer, "gc") else renderer.ctx
self._surf.context = ctx
self._surf.cairo.show_page()
...
fig = plt.figure()
ax = fig.gca()
surf_artist = SurfArtist(surf)
surf_artist.set_zorder(float('inf'))
ax.artists.append(surf_artist)