Python code:
import base64
from io import BytesIO
def fig_to_html(fig):
"""
Convert a matplotlib.Figure object to html image source
:param fig:
:return: the returned value can be used in html like this: <img src="data:image/png;base64,<return value>"/>
"""
buf = BytesIO()
fig.savefig(buf, format="png")
figure = base64.b64encode(buf.getbuffer()).decode("ascii")
return figure
HTML code:
<img src="data:image/png;base64,{{ figure }}"/>