Draw plot into existing cairo context

Hi, I’ve got an existing pycairo Context object ( Cairo Context — Pycairo documentation )

How can I ask matplotlib to draw a plot into that context?


You can probably do something approximately like

from matplotlib.backends.backend_cairo import RendererCairo

fig = <the-figure>
renderer = RendererCairo(fig.dpi)
renderer.gc.ctx = <the-context>
renderer.set_width_height(width, height)

Note that this API is going to change in Matplotlib 3.6 into something a bit simpler:

renderer = RendererCairo(fig.dpi)
renderer.set_context(<the-context>)  # Size is auto-inferred from context