仅在tkinter中打开图(无matplotlib弹出窗口) [英] Opening a plot in tkinter only (no matplotlib popup)

查看:56
本文介绍了仅在tkinter中打开图(无matplotlib弹出窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在 tkinter 窗口中打开一个 matplotlib pyplot.我的问题是情节仍然在 matplotlib 窗口中弹出,我不知道如何阻止它.我尝试注释掉每个plt.plot的内容,但这无济于事.

I am opening a matplotlib pyplot in a tkinter window with the below code. My issue is that the plot is still popping up in the matplotlib window as well, and I don't know how to stop that. I've tried commenting out each of the plt.plot's but that didn't help.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import Tkinter as Tkinter

def tester():

    class window(Tkinter.Tk):
        def __init__(self,parent):
            Tkinter.Tk.__init__(self,parent)
            self.parent = parent
            self.initialize()

        def initialize(self):
            self.grid()
            self.grid_rowconfigure(0,minsize=50)
            self.grid_rowconfigure(1,minsize=5)
            self.grid_rowconfigure(2,minsize=500)
            self.grid_rowconfigure(3,minsize=5)
            self.grid_rowconfigure(4,minsize=20)

            self.grid_columnconfigure(0,minsize=5)
            self.grid_columnconfigure(1,minsize=800)
            self.grid_columnconfigure(2,minsize=5)

            framer = Tkinter.Frame(self, borderwidth=2, relief='groove',bg='white')
            framer.grid(column=0,row=1,columnspan=3,rowspan=3, sticky='NSEW')
            Button1 = Tkinter.Button(bg='red',text="Click Me",command=self.onbutton1)
            Button1.grid(column=1,row=0,sticky='NSEW')


        def onbutton1(self):
            array = np.array([[1,2,3,2,1],[2,3,4,3,2],[3,4,5,4,3],[4,5,6,5,4],[3,4,5,4,3]])
            maximum = np.max(array)
            index_max = np.where(array == maximum)
            max_a, max_b = index_max  

            plotter=plt.figure('plot')            
            plt.contour(array, linewidths = 1, colors = 'k')
            plt.contourf(array, cmap = plt.cm.jet)
            plt.ylabel('y', fontdict = {'fontsize':16})
            plt.xlabel('x', fontdict = {'fontsize':16})
            plt.colorbar()
            plt.title('Title', fontdict = {'fontsize':20})    
            plt.plot(max_b, max_a, 'wo')
            F_canvas = FigureCanvasTkAgg(plotter, self)
            F_canvas.get_tk_widget().grid(column=1,row=2)


    if __name__ == "__main__":
        app = window(None)
        app.title('Window')
        app.mainloop()

tester()

谁能成为我的英雄?

推荐答案

如果你想自己管理所有的窗口,你根本不应该使用 pyplot.例如,当您执行 plt.figure 时,它将创建一个由matplotlib管理的图形窗口.

If you want to manage all the windows yourself, you should not use pyplot at all. When you do plt.figure, for instance, it is creating a matplotlib-managed figure window.

甚至不要导入pyplot.具体如何调整您的代码取决于您使用的 matplotlib 功能,但例如,要创建您想要执行的图形:

Don't even import pyplot. Exactly how to adapt your code will vary depending on what matplotlib features you use, but, for instance, to create the figure you'll want to do:

from matplotlib import figure
plotter = figure.Figure()

您将通过访问该图形的 Axes 对象并使用其方法(例如,如果您有一个轴 ax)来访问大多数绘图类型(例如 contour)代码>你可以调用ax.contour).

You'll access most of the plot types (such as contour) by accessing an Axes object of that Figure and using its methods (e.g., if you have an axes ax you can cal ax.contour).

有关如何将matplotlib嵌入到Tkinter应用程序中的简单演示,请参见此示例

See this example for a simple demo of how to embed matplotlib in a Tkinter app.

这篇关于仅在tkinter中打开图(无matplotlib弹出窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆