Update matplotlib chart

I am trying to update the matplotlib graph as follows:

import matplotlib.pyplot as plt import matplotlib.dates as mdate import numpy as np plt.ion() fig = plt.figure() ax = fig.add_subplot(111) for i,(_,_,idx) in enumerate(local_minima): dat = dst_data[idx-24:idx+25] dates,values = zip(*dat) if i == 0: assert(len(dates) == len(values)) lines2d, = ax.plot_date(mdate.date2num(dates), np.array(values), linestyle='-') else: assert(len(dates) == len(values)) lines2d.set_ydata(np.array(values)) lines2d.set_xdata(mdate.date2num(dates)) #This line causes problems. fig.canvas.draw() raw_input() 

For the first time through a cycle, the graph is displayed just fine. The second time through the cycle, all the data on my graph disappears - everything works fine if I do not include the lines2d.set_xdata line (except, of course, the x-data points). I looked at the following posts:

How to update the plot in matplotlib?

and

Update rows in matplotlib

However, in both cases, the user only updates ydata , and I would also like to update xdata .

+4
source share
1 answer

As in a typical case, the act of writing a question made me think about an opportunity that I had not thought of before. X data is updated, but there are no graph ranges. When I put new data on the plot, it was all out of reach. The solution was to add:

 ax.relim() ax.autoscale_view(True,True,True) 

(partial link)


Here's the code in the context of the original question in the hope that it will be useful to someone else someday:

 import matplotlib.pyplot as plt import matplotlib.dates as mdate import numpy as np plt.ion() fig = plt.figure() ax = fig.add_subplot(111) for i,(_,_,idx) in enumerate(local_minima): dat = dst_data[idx-24:idx+25] dates,values = zip(*dat) if i == 0: assert(len(dates) == len(values)) lines2d, = ax.plot_date(mdate.date2num(dates), np.array(values), linestyle='-') else: assert(len(dates) == len(values)) lines2d.set_ydata(np.array(values)) lines2d.set_xdata(mdate.date2num(dates)) #This line causes problems. ax.relim() ax.autoscale_view(True,True,True) fig.canvas.draw() raw_input() 
+4
source

Source: https://habr.com/ru/post/1442569/


All Articles