新图形未显示在画布PySimpleGUI和Matplotlib上

2024-09-28 21:12:18 发布

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

我需要根据两个按钮重置图形。如果按下按钮1,则显示第一个图形,但不显示按下按钮2后的第二个图形

第一个图形显示正确,但需要清除画布

代码如下:

import matplotlib.pyplot as plt
import matplotlib
import numpy
import numpy as np
import PySimpleGUI as psg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use('TkAgg')

def make_graph_and_put_on_canvas(x, y, xlabel, ylabel, graph_title, canvas):

    figure,ax=plt.subplots()
    ax.plot(x, y)
    ax.set(xlabel=xlabel, ylabel=ylabel,
           title=graph_title)
    ax.grid()

    figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
    figure_canvas_agg.draw()
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)

    return figure_canvas_agg

if __name__ == '__main__':
   layout = [[psg.B("Button1"),psg.B("Button2")],[psg.Canvas(key="canvas")]]
   Graph = psg.Window(title="Graph", layout=layout, size=(500, 500))
   while (True):
        event, Value = Graph.read()
        if event == psg.WINDOW_CLOSED:
            Graph.close()
            break
        if event=="Button1":
            #Make the first graph for y=2x
            x=[0,1,2,3]
            y=[0,2,4,6]
            make_graph_and_put_on_canvas(x, y, "x", "y", "title",Graph["canvas"].TKCanvas)

        if event=="Button2":
            # Make the first graph for y=3x
            x = [0, 1, 2, 3]
            y = [0, 3, 6, 9]
            make_graph_and_put_on_canvas(x, y, "x", "y", "title",Graph["canvas"].TKCanvas)

Tags: importevent图形iftitlematplotlibasax
1条回答
网友
1楼 · 发布于 2024-09-28 21:12:18

FigureCanvasTkAgg(figure, canvas)将在Graph["canvas"].TKCanvas中创建一个新画布。 使用选项side='top'pack时,您将从Graph["canvas"].TKCanvas的顶部到底部获得新画布。第二个Button确实生成了第二个图形,但位于窗口底部,对于窗口中的size = (500, 500)不可见

  • 如果希望在同一图形上显示新图形,请调用以下代码一次
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
  • 用手指划出图形或轴
clf()    # clear figure
cla()    # clear axis
  • 完成图形上的所有绘图后,更新画布上的图形
figure_canvas_agg.draw()

下面的代码只是工作,没有经过优化

import matplotlib.pyplot as plt
import matplotlib
import numpy
import numpy as np
import PySimpleGUI as psg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

matplotlib.use('TkAgg')

def make_graph_and_put_on_canvas(x, y, xlabel, ylabel, graph_title, canvas):

    ax.cla()
    ax.plot(x, y)
    ax.set(xlabel=xlabel, ylabel=ylabel, title=graph_title)
    ax.grid()
    figure_canvas_agg.draw()

    return figure_canvas_agg

if __name__ == '__main__':

    layout = [[psg.B("Button1"),psg.B("Button2")],[psg.Canvas(key="canvas")]]
    Graph = psg.Window(title="Graph", layout=layout, size=(500, 500), finalize=True)
    figure, ax = plt.subplots()
    canvas = Graph["canvas"].Widget
    figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)

    while (True):

        event, Value = Graph.read()

        if event == psg.WINDOW_CLOSED:
            break

        if event=="Button1":
            # Make the first graph for y=2x
            x=[0,1,2,3]
            y=[0,2,4,6]
            make_graph_and_put_on_canvas(x, y, "x", "y", "title1", canvas)

        if event=="Button2":
            # Make the first graph for y=3x
            x = [0, 1, 2, 3]
            y = [0, 3, 6, 9]
            make_graph_and_put_on_canvas(x, y, "x", "y", "title2", canvas)

    Graph.close()

相关问题 更多 >