Skip to content

Instantly share code, notes, and snippets.

@nlothian
Created February 18, 2021 02:33
Show Gist options
  • Save nlothian/6520cf8b35b4ff69e3148806db6f17f5 to your computer and use it in GitHub Desktop.
Save nlothian/6520cf8b35b4ff69e3148806db6f17f5 to your computer and use it in GitHub Desktop.
Convert a matplotlib graph to base64, suitable for inclusing as a data: url in HTML.
def build_GRAPHS_MD(fig):
# encode as base64 for markdown display
buf = io.BytesIO()
fig.savefig(buf, format='png')
buf.seek(0)
b64str = base64.b64encode(buf.read()).decode('ascii')
plt.close()
markdown_txt = f"""
![Distribution](data:image/png;base64,{b64str})
` `
` `
"""
return markdown_txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment