Skip to content

Instantly share code, notes, and snippets.

@jbn
Created February 28, 2018 16:00
Show Gist options
  • Save jbn/58de053e81ad305621dee1e1bf272232 to your computer and use it in GitHub Desktop.
Save jbn/58de053e81ad305621dee1e1bf272232 to your computer and use it in GitHub Desktop.
Extract for attempted David assist
import matplotlib.pyplot as plt
from ipykernel.pylab.backend_inline import flush_figures
from IPython.display import display, clear_output, SVG
def replot_as_svg(fig=None, **adjustments):
"""
Take a given matplotlib figure and write it as an embeddable SVG.
:param fig: the matplotlib figure or None for implicit use of plt.gcf()
:return: an IPython SVG object.
"""
if fig is None:
fig = plt.gcf()
if adjustments:
fig.subplots_adjust(**adjustments)
buf = BytesIO()
fig.savefig(buf, format="svg")
plt.close()
buf.seek(0)
clear_output(wait=True)
svg = SVG(data=hatch_safe_svg(buf.read()))
display(svg)
return svg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment