Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 新图形未显示在画布PySimpleGUI和Matplotlib上_Python_Pysimplegui - Fatal编程技术网

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

Python 新图形未显示在画布PySimpleGUI和Matplotlib上,python,pysimplegui,Python,Pysimplegui,我需要根据两个按钮重置图形。如果按下按钮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 matplo

我需要根据两个按钮重置图形。如果按下按钮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)
FigureCanvasTkAgg(图,画布)
将在
Graph[“canvas”]中创建一个新画布。TKCanvas
。 使用选项
side='top'
pack
时,您将从
图形[“canvas”]的顶部到底部获得新画布。TKCanvas
。第二个
按钮
确实生成了第二个图形,但位于窗口底部,对于窗口中的
大小=(500500)
不可见

  • 如果希望在同一图形上显示新图形,请调用以下代码一次
figure\u canvas\u agg=FigureCanvasTkAgg(图,canvas)
图\u canvas\u agg.get\u tk\u widget().pack(side='top',fill='both',expand=1)
  • 用手指划出图形或轴
clf()#清晰的数字
cla()#清除轴
  • 完成图形上的所有绘图后,更新画布上的图形
figure\u canvas\u agg.draw()
下面的代码只是工作,没有经过优化

导入matplotlib.pyplot作为plt
导入matplotlib
进口numpy
将numpy作为np导入
将PySimpleGUI导入为psg
从matplotlib.backends.backend_tkagg导入图CAVASTKAGG
matplotlib.use('TkAgg')
def make_graph_和put_放在画布上(x、y、xlabel、ylabel、graph_title、canvas):
ax.cla()
轴图(x,y)
ax.set(xlabel=xlabel,ylabel=ylabel,title=graph\u title)
ax.grid()
图_canvas_agg.draw()
返回图形\u画布\u agg
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
布局=[[psg.B(“按钮1”)、psg.B(“按钮2”)]、[psg.Canvas(key=“Canvas”)]]
Graph=psg.Window(title=“Graph”,layout=layout,size=(500500),finalize=True)
图,ax=plt.子批次()
画布=图形[“画布”]。小部件
figure_canvas_agg=FigureCanvasTkAgg(图,画布)
图\u canvas\u agg.get\u tk\u widget().pack(side='top',fill='both',expand=1)
虽然(正确):
事件,值=Graph.read()
如果事件==psg.WINDOW\u关闭:
打破
如果事件==“按钮1”:
#制作y=2x的第一个图形
x=[0,1,2,3]
y=[0,2,4,6]
制作图形并将其放在画布上(x,y,“x”,“y”,“标题1”,画布)
如果事件==“按钮2”:
#制作y=3x的第一个图形
x=[0,1,2,3]
y=[0,3,6,9]
制作图形并将其放在画布上(x,y,“x”,“y”,“title2”,画布)
Graph.close()

感谢您提供的解决方案。但是,我不知道如何使用它。如果您能对上面共享的代码进行更改,我将不胜感激。我想一次在画布上绘制一个图形。但是图形应该根据按钮的不同而变化。如上所述进行更新。现在可以工作了。非常感谢。除了您的编辑,我在代码中错过的一个重要步骤是在
sg.Window
命令中添加
finalize=True
。由于我有
window.read()
,我不知道为什么有必要这样做。无论如何,非常感谢您的帮助。在窗口完成或
window.read()
之前,tkinter未初始化且画布仍然不存在,因此您无法将matplotlib与画布链接。