Hi there!
I’ve been wondering, what is the best way to return a matplotlib figure from the callback function and get it visualized in a Dash app properly?
The only working way I’ve been able to find was base64 encoding of the plot followed up by passing the decoded hash to the html.Img src parameter. However, this solution doesn’t work stable for me. Namely, two graphs which should be displayed next to each other tend to interchangeably not appear at all from time to time.
I’d be grateful if you could give me a hint, whether there’re some alternative solutions, or maybe point at my mistake!
Here’s an example of the callback I’m using.
@app.callback(Output(component_id='distribution-map', component_property='src'),
Input(component_id='my-slider', component_property='value'))
def distribution(value):
s1 = io.BytesIO()
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.EqualEarth()}, figsize=(8, 8))
load_distribution = # data fetching
network.plot(bus_sizes=1e-5*load_distribution, ax=ax, title="Load distribution")
plt.close()
plt.savefig(s1, format='png')
s1 = base64.b64encode(s1.getvalue()).decode("utf-8").replace("\n", "")
return f'data:image/png;base64,{s1}'
Maybe, the problem is that the network.plot() method of pypsa returns the plot itself preventing plt.savefig() from saving the plot contents?
Thanks in advance for your answer and help!