I am creating a matplotlib animation to be displayed on a flask app based on user input. The matplotlib script is similar to this:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
# Horizontal bar plot with gaps
fig, ax = plt.subplots()
ax.get_yaxis().set_visible(False)
ax.spines[['top', 'bottom','left','right']].set_visible(False)
y2=[20,20,20,20,20,20,20]
y3=np.array(y2) #convert to array wont work with list
x2 =[20,15,14,13, 12,11,10]
x3=np.array(x2)
year =["2014","2015","2016","2017","2018","2019","2020"]
yr2 =np.array(year)
def animate(i):
ax.clear()
ax.set_ylim(16, 24)
ax.barh(20, 60, 4 )
ax.plot(60, 18, marker=6, markersize=18, clip_on=False,)
ax.annotate(r"$\bf" + str(2013) +"$" + f" ({60})", (60 , 18),xytext=(0, -25), size= 8, textcoords='offset points', ha='center', va='bottom')
ax.barh(y3[i], x3[i], 4,color='c')
ax.plot(x3[i], y3[i]+2, color = 'c', marker=7, markersize=18, clip_on=False,)
ax.annotate(r"$\bf" + str(yr2[i]) +"$" + f" ({x3[i]})", (x3[i] , y3[i]+2),xytext=(0, 15), size= 8, color = 'c', textcoords='offset points', ha='center', va='bottom')
ani = animation.FuncAnimation(fig, animate, repeat=False,
frames=len(x3), interval=100000)
# To save the animation using Pillow as a gif
writer = animation.PillowWriter(fps=1,
metadata=dict(artist='Me'),
bitrate=1800)
ani.save('scatter.gif', writer=writer)
Is it possible to save gif into an in-memory file rather than saving as a gif?
tempfile
, load it into memory withio.BytesIO
, and delete the file.buf = io.BytesIO()
andani.save(buf, writer=writer)
, becauseani.save
does not acceptBytesIO
as the path.python 3.9.18
,flask 2.2.2
,matplotlib 3.7.2
,numpy 1.21.5
.Updates per comments from OP based on Given a BytesIO buffer, generate img tag in html.