How to update seaborn data when I use django?

I use django and create web page. Then I click button seaborn do not update data, only add data. Scatters are added to the plot as many times as I click the button. However I want reset existing data and show new plot by new data.

def index(request):
   if request.method == "GET":

      form_index = ForecastField(request.GET)
       if form_index.is_valid():
         adress_f = form_index.cleaned_data['flat_Ad']
         newdf_pl = pd.read_csv(r'newdf.csv')
         newdf_pl = newdf_pl.loc[newdf_pl['Address_N'] == adress_f]

         sns.scatterplot(data=newdf_pl, x='Area', y='Price_G')
         fig = plt.gcf()
         buff = BytesIO()
         fig.savefig(buff, format='png')
         buff.seek(0)
         string = base64.b64encode(buff.read())
         uri_F = urllib.parse.quote(string)

         context = {'Index_f': ForecastField(), 'pngg_F': uri_F}

         html_index = "MyHome/index.html"
         return render(request, html_index, context)

What is this sns object you’re referencing at sns.scatterplot(data=newdf_pl, x='Area', y='Price_G')? Where is it initialized? How is the view getting access to it?

Thanks for the feedback. I do not understand How is the view getting access to it. I view plot in web page. how can I initialize and update data in sns, every button click? I understand that need initialize, however how?

My HTML code:

< img src=‘data:image/png;base64, {{ pngg_F }}’ alt=“image”/>

You posted:

You have this line in your code:
sns.scatterplot(data=newdf_pl, x='Area', y='Price_G')

This means you are calling the scatterplot method of the sns object.

However, in the portion of the code you posted, I don’t see where this sns object is defined or passed to the view. Where does it come from?

I’m guessing there’s a lot more to this code than what you’ve posted here.

Before I can answer your question about your diagrams, I need to understand a lot more about how you’re creating them.

Yes, I understand and I am also surprised that it passes the view without assignment. This is probably where the problem comes from, because it doesn’t update every time.

Right off the top of my head, I think the biggest problems are the module-level objects being created and accessed directly.

Regardless of what you do here, you’re going to have some degree of a race condition existing within your system. (See my comment at Matplotlib in Django "Starting a Matplotlib GUI outside of the main thread..." - #5 by KenWhitesell)

About the best / safest solution I can think of would be to set up an external worker to process these requests, making sure the data components are initialized at the beginning of each request. (It might even be better / easier to launch a separate process to do this on each request. That way you can ensure that there’s no “global state” to be concerned about.)

But the bottom line is that I don’t expect that you’re going to find a clean way of doing this directly “in-line” as you’re trying to do here. Those components (pandas and matplotlib) are not particularly “friendly” to the Django environment.

yes, i am understand. you recommendation is that i will use chart.js. this is news with me and i will learn few features with javascript.

To be clear, I am not recommending that you use chart.js.

Yes, using chart.js is an option.

However, if you really want to use the combination of Pandas, Matplotlib, and Seaborn, you can. It’s just going to require a bit more work on your part to make it stable and reliable. But it can be done.

It’s your choice where to spend your time and effort.

Thanks for the help. Obviously the Python visualization libraries require more work. I tried chart.js and it’s ok.