Python Matplotlib后台更新Plot

2024-04-19 22:14:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Matplotlib在Anaconda提示符中绘制实时事件。
当我更新绘图依据时绘图()或表演(),我对我正在做的事情失去了控制。“绘图”窗口的行为与单击窗口类似,这会阻止命令提示符上的其他控件。在

我试着补充

plt.show(block=False)

但没用。在

代码如下所示

^{pr2}$

Tags: 代码false绘图matplotlibshow绘制事件plt
1条回答
网友
1楼 · 发布于 2024-04-19 22:14:16

这个link有一个使用matplotlib实时打印的示例。我认为最重要的是你不需要使用表演()或绘图()每次打电话来策划。该示例使用set_ydata代替。Simalarly set_xdata可用于更新x轴变量。以下代码

import matplotlib.pyplot as plt
import numpy as np

# use ggplot style for more sophisticated visuals
plt.style.use('ggplot')

def live_plotter(x_vec,y1_data,line1,identifier='',pause_time=0.1):
    if line1==[]:
        # this is the call to matplotlib that allows dynamic plotting
        plt.ion()
        fig = plt.figure(figsize=(13,6))
        ax = fig.add_subplot(111)
        # create a variable for the line so we can later update it
        line1, = ax.plot(x_vec,y1_data,'-o',alpha=0.8)        
        #update plot label/title
        plt.ylabel('Y Label')
        plt.title('Title: {}'.format(identifier))
        plt.show()

    # after the figure, axis, and line are created, we only need to update the y-data
    line1.set_ydata(y1_data)
    # adjust limits if new data goes beyond bounds
    if np.min(y1_data)<=line1.axes.get_ylim()[0] or np.max(y1_data)>=line1.axes.get_ylim()[1]:
    plt.ylim([np.min(y1_data)-np.std(y1_data),np.max(y1_data)+np.std(y1_data)])
# this pauses the data so the figure/axis can catch up - the amount of pause can be altered above
plt.pause(pause_time)

# return line so we can update it again in the next iteration
return line1

当我在下面的例子中运行这个函数时,在我的计算机上使用其他应用程序没有任何问题

^{pr2}$

相关问题 更多 >