简体   繁体   English

完全更新matplotlib图(不追加数据)

[英]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.编辑:我通过完全省略 matplotlib animation 并在初始化数字之前添加plt.ion()来使其工作。

I have a measurement device that sends data to my PC that I can read with Python.我有一个可以将数据发送到我的 PC 的测量设备,我可以使用 Python 读取这些数据。 Every second it sends 100 data points.它每秒发送 100 个数据点。 Every time these 100 points are received, they can be obtained by calling a function.每次收到这100分,都可以通过调用function来获得。 I want to update the figure, starting from X=0 again.我想更新图形,再次从 X=0 开始。 The data contains X and Y data.数据包含 X 和 Y 数据。 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.在其他 SO 线程中,我发现matplotlib.animation.FuncAnimation可以有效地更新图表,但我只找到了将数据附加到图表的示例。
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).目前,我的图表正在重绘,但它只是绘制新点而没有清除之前的图表,而且速度非常慢(我无法在绘制时拖动 plot window)。 The clear function just leaves me with a grey window.清晰的 function 只给我留下了灰色的 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. figInitProperties返回图形的fig (大小、标签、轴、刻度等)。

You can use ax.clear() or plt.cla() at the beginning of the update function.您可以在更新 function 的开头使用ax.clear()plt.cla()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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