简体   繁体   中英

Completely update matplotlib figure (not appending data)

Edit: I got it to work by omitting matplotlib animation completely and adding plt.ion() before initializing the figures.

I have a measurement device that sends data to my PC that I can read with Python. Every second it sends 100 data points. Every time these 100 points are received, they can be obtained by calling a function. I want to update the figure, starting from X=0 again. The data contains X and Y data. In other SO threads I found that matplotlib.animation.FuncAnimation can update the graph efficiently, but I have only found examples where data is appended to the graph.
At the moment, my graph is redrawing, but it just draws new points without clearing the previous graph and it is really slow (I can't drag the plot window while it's drawing). The clear function just leaves me with a grey window. Does anyone know a fast enough workaround for this problem? Thanks a lot!

My current implementation:

def plot():
    freq = np.linspace(f_start, f_stop, points, endpoint=True)

    def update(frame):
        #for i in range (iterations - 1):
        print(frame)
        complex_data = vna.data(0)
        plotFigure(figInitProperties, freq, Rect2Pol(complex_data))
        time.sleep(2) # Wait for all points to be scanned

    ani = anim.FuncAnimation(figInitProperties[2], func=update, frames=(10), repeat=False)
    plt.show()
plot()

figInitProperties returns the fig (size, labels, axis, ticks etc.) of the figure.

You can use ax.clear() or plt.cla() at the beginning of the update function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM