matplotlib不会使用按钮实现进行打印

2024-03-29 11:59:06 发布

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

下面是多线程程序的一部分。全局变量AdjustedTargetTaskHistory和TimeStampHistory从程序的另一部分更新,并且是扁平列表,分别显示在y轴和x轴上。回调允许图形每2秒更新一次。你知道吗

我试图在图中添加一个按钮的实现。函数buttonPush()实际运行并打印文本。但是,该图不会绘制。你知道吗

def buttonPush(event):
    print("Pushed the button!")

def plotGraph():
    global adjustedTargetAskHistory
    global TimeStampHistory

    plt.ion()
    fig = plt.figure()
    axes = fig.add_subplot(111)
    plt.xlabel('Time')

    ax = plt.axes([0.0, 0.0, 0.2, 0.1])
    bcut = Button(ax,'Update', color='red', hovercolor='green')
    bcut.on_clicked(buttonPush)

    axes.set_autoscale_on(True)
    axes.autoscale_view(True,True,True)    

    adjustedTargetAskli, = plt.plot([], [], 'r-')

    while True:
        adjustedTargetAskli.set_data(TimeStampHistory,adjustedTargetAskHistory)
        axes.relim()
        axes.autoscale_view(True,True,True)
        plt.pause(2)

问题似乎在于以下几行,因为如果没有它们,绘图将无缝更新。你知道吗

ax = plt.axes([0.0, 0.0, 0.2, 0.1])
bcut = Button(ax,'Update', color='red', hovercolor='green')
bcut.on_clicked(buttonPush)

工作(无按钮) enter image description here

不工作(带按钮) enter image description here


Tags: 程序trueondeffigpltax按钮