I am building a web application using Django that has an option of plotting plots like histograms, scatterplots, bar charts etc
Using matplotlib lib, I am plotting the plots and rendering the plots to HTML pages.
plt.figure()
plt.title("xyz")
plt.tight_layout()
plt.plot(x,y, 'b')
plt.plot(x,z, 'r')
buf = BytesIO()
fig = plt.gcf()
fig.set_size_inches(12,8, forward=True)
fig.savefig(buf, format='png')
plt.clf()
# Get Image
image_base64 = base64.b64encode(
buf.getvalue()).decode('utf-8').replace('\n', '')
img_src = 'data:image/png;base64, {}'.format(image_base64)
When a user sends two different requests to plot different plots, the content like legend and data points are mixing with other plots and results in an overlap of plots. In the attached image, the plot on the left side should be similar to the plot on the right side. But the content of different plot request is appended to this response and being displayed in this response.