Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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 如何将matplotlib折线图添加到Tkinter GUI?_Python_Matplotlib_Tkinter - Fatal编程技术网

Python 如何将matplotlib折线图添加到Tkinter GUI?

Python 如何将matplotlib折线图添加到Tkinter GUI?,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我想知道如何将matplotlib折线图添加到tkinter。这是我的密码: import matplotlib.pyplot as plt from tkinter import * import tkinter as tk from tkinter import ttk from pylab import * root = tk.Tk() root.geometry('500x700') firstentry = StringVar() secondentry = StringVar()

我想知道如何将matplotlib折线图添加到tkinter。这是我的密码:

import matplotlib.pyplot as plt
from tkinter import *
import tkinter as tk
from tkinter import ttk
from pylab import *

root = tk.Tk()
root.geometry('500x700')

firstentry = StringVar()
secondentry = StringVar()


class Random():
    def _init_(self):
        self.label1 = None
        self.label2 = None
        self.userEntry = None
    def firstentry(self):
        self.label1 = ttk.Label(text="Enter:")
        self.label1.pack()
        self.userEntry = ttk.Entry(textvariable=firstentry)
        self.userEntry.pack()
        self.button_2 = Button(root, text="Enter", command=self.secondentry)
        self.button_2.pack()
    def secondentry(self):
        self.label2 = ttk.Label(text="Enter:")
        self.label2.pack()
        self.userEntry = ttk.Entry(textvariable=secondentry)
        self.userEntry.pack()
        self.button_3 = Button(root, text="Enter", command=self.line_chart)
        self.button_3.pack()
    def line_chart(self):
        pass

random = Random()

button_1 = Button(root, text="button", command=lambda:random.firstentry())
button_1.pack()

root.mainloop()

我希望程序在用户发送第二个输入后显示折线图,以便折线图功能在发送输入后显示它。然而,我不知道怎么做。有人能解释一下我是如何嵌入它的吗?

如果你在发布之前花点时间去谷歌看看,互联网上有很多文档

无论如何

这里有一个例子
将matplotlib导入为mpl
将numpy作为np导入
导入系统
如果系统版本信息[0]<3:
将Tkinter作为tk导入
其他:
将tkinter作为tk导入
将matplotlib.backends.tkagg作为tkagg导入
从matplotlib.backends.backend_agg导入图Canvasagg
def draw_图形(画布,图形,位置=(0,0)):
figure_canvas_agg=FigureCanvasAgg(图)
图_canvas_agg.draw()
图x、图y、图w、图h=figure.bbox.bounds
图w,图h=int(图w),int(图h)
photo=tk.PhotoImage(母版=canvas,宽度=figure\u w,高度=figure\u h)
#位置:从左上角锚点转换为中心锚点
画布。创建图片(位置[0]+图w/2,位置[1]+图h/2,图片=照片)
#不幸的是,没有指向本机渲染器指针的访问器
blit(照片,图形画布,图像获取渲染器()。\u渲染器,颜色模式=2)
#返回包含对照片对象引用的句柄
#必须保持现场直播,否则照片就会消失
返回照片
#创建画布
w、 h=300200
window=tk.tk()
窗口标题(“画布中的人物”)
canvas=tk.canvas(窗口,宽度=w,高度=h)
canvas.pack()
#生成一些示例数据
X=np.linspace(0,2*np.pi,50)
Y=np.sin(X)
#创建我们希望添加到现有画布的图形
fig=mpl.figure.figure(figsize=(2,1))
ax=图添加轴([0,0,1,1])
轴图(X,Y)
#保持此手柄处于活动状态,否则图形将消失
图x,图y=100100
fig_photo=绘制图(画布,图,位置=(图x,图y))
fig_w,fig_h=fig_photo.width(),fig_photo.height()
#向画布中添加更多元素,可能位于图形顶部
画布。创建线(200,50,图x+图w/2,图y+图h/2)
画布。创建_文本(200,50,text=“零交叉”,anchor=“s”)
#让Tk接管
tk.mainloop()

你显然没有读我的帖子。这与我的问题无关。在回答问题之前,请先阅读问题。@Omar您的问题说明:如何将matplotlib折线图添加到Tkinter GUI中
import matplotlib as mpl
import numpy as np
import sys
if sys.version_info[0] < 3:
  import Tkinter as tk
else:
  import tkinter as tk
  import matplotlib.backends.tkagg as tkagg
  from matplotlib.backends.backend_agg import FigureCanvasAgg


def draw_figure(canvas, figure, loc=(0, 0)):
  figure_canvas_agg = FigureCanvasAgg(figure)
  figure_canvas_agg.draw()
  figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
  figure_w, figure_h = int(figure_w), int(figure_h)
  photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)

  # Position: convert from top-left anchor to center anchor
  canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)

  # Unfortunately, there's no accessor for the pointer to the native renderer
  tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)

  # Return a handle which contains a reference to the photo object
  # which must be kept live or else the picture disappears
  return photo

# Create a canvas
w, h = 300, 200
window = tk.Tk()
window.title("A figure in a canvas")
canvas = tk.Canvas(window, width=w, height=h)
canvas.pack()

# Generate some example data
X = np.linspace(0, 2 * np.pi, 50)
Y = np.sin(X)

# Create the figure we desire to add to an existing canvas
fig = mpl.figure.Figure(figsize=(2, 1))
ax = fig.add_axes([0, 0, 1, 1])
ax.plot(X, Y)

# Keep this handle alive, or else figure will disappear
fig_x, fig_y = 100, 100
fig_photo = draw_figure(canvas, fig, loc=(fig_x, fig_y))
fig_w, fig_h = fig_photo.width(), fig_photo.height()

# Add more elements to the canvas, potentially on top of the figure
canvas.create_line(200, 50, fig_x + fig_w / 2, fig_y + fig_h / 2)
canvas.create_text(200, 50, text="Zero-crossing", anchor="s")

# Let Tk take over
tk.mainloop()