Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 特金特散点图_Python_User Interface_Matplotlib_Tkinter_Scatter Plot - Fatal编程技术网

Python 特金特散点图

Python 特金特散点图,python,user-interface,matplotlib,tkinter,scatter-plot,Python,User Interface,Matplotlib,Tkinter,Scatter Plot,我正在尝试使用tkinter创建一个GUI应用程序,要求用户输入x和y值,完成后,单击生成散点图的按钮。当我运行代码时,什么都没有发生,我没有收到错误消息,没有弹出窗口,我也不确定出了什么问题。这是我的密码: import matplotlib matplotlib.use('TkAgg') import numpy as np from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figu

我正在尝试使用tkinter创建一个GUI应用程序,要求用户输入x和y值,完成后,单击生成散点图的按钮。当我运行代码时,什么都没有发生,我没有收到错误消息,没有弹出窗口,我也不确定出了什么问题。这是我的密码:

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

class mclass:
    mylist = []
    mylist1 = []

    def __init__(self, window):
        self.window = window
        self.box1 = Entry(window)
        self.button1 = Button(window, text="enter", command=lambda:     get_data1(mylist), width=5)
        self.button1.grid(row=0, column=2)
        self.box1.pack()
        self.button1.pack()
        self.box2 = Entry(window)
        self.button2 = Button(window, text="enter", command=lambda: get_data2(mylist1), width=5)
        self.button2.grid(row=1, column=2)
        self.box2.pack()
        self.button2.pack()
        self.box3 = Entry(window)
        self.button3 = Button(window, text="Finish & Plot", command=self.plot)
        self.box3.pack()
        self.button3.pack()


        def get_data1(l):
            l.append(self.box1.get())
            print(l)

        def get_data2(l1):
            l1.append(self.box2.get())
            print(l1)

        var1 = StringVar()
        var1.set("X Values:")
        label1 = Label(window, textvariable=var1, height=2)
        label1.grid(row=0, column=0)


       ID1 = StringVar()
       self.box1 = Entry(window, bd=4, textvariable=ID1)
       self.box1.grid(row=0, column=1)

       var2 = StringVar()
       var2.set("Y Values:")
       label2 = Label(window, textvariable=var2, height=2)
       label2.grid(row=1, column=0)

       ID2 = StringVar()
       self.box2 = Entry(window, bd=4, textvariable=ID2)
       self.box2.grid(row=1, column=1)


    def plot(self):
        x = np.array(mylist)
        v = np.array(mylist1)

        fig = Figure(figsize=(6, 6))
        a = fig.add_subplot(111)
        a.scatter(v, x, color='red')


        a.set_title ("Scatter Plot)", fontsize=16)
        a.set_ylabel("Y", fontsize=14)
        a.set_xlabel("X", fontsize=14)

        canvas = FigureCanvasTkAgg(fig, master=self.window)
        canvas.get_tk_widget().pack()
        canvas.draw()

window = Tk()
start = mclass(window)
window.mainloop()

如果有人能帮我解决我的错误,我将不胜感激,谢谢

老实说,您的原始示例中有很多内容不正确,我收到了很多错误消息。它们中的大多数都是将pack和grid一起使用的,这在一个框架内是不可能的。这是您的代码的工作版本

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

class mclass:

    def __init__(self, window):
        self.mylist = []
        self.mylist1 = []

        self.window = window

        self.button3 = Button(window, text="Finish & Plot", command=self.plot)
        self.button3.pack()

        var1 = StringVar()
        var1.set("X Values:")
        label1 = Label(window, textvariable=var1, height=2)
        label1.pack()


        ID1 = StringVar()
        self.box1 = Entry(window, bd=4, textvariable=ID1)
        self.box1.pack()

        var2 = StringVar()
        var2.set("Y Values:")
        label2 = Label(window, textvariable=var2, height=2)
        label2.pack()

        ID2 = StringVar()
        self.box2 = Entry(window, bd=4, textvariable=ID2)
        self.box2.pack()

        self.fig = Figure(figsize=(6, 6))
        self.a = self.fig.add_subplot(111)

        self.canvas = FigureCanvasTkAgg(self.fig, master=self.window)
        self.canvas.get_tk_widget().pack()

    def read_inputs(self):
        x_input = self.box1.get()
        y_input = self.box2.get()


        def convert_to_float_list(x_in):
            x_input_list = x_in.split(',')
            x_floats = [float(x) for x in x_input_list]
            return x_floats


        x_array = np.array(convert_to_float_list(x_input))
        y_array = np.array(convert_to_float_list(y_input))

        return x_array,y_array


    def plot(self):
        self.a.cla()
        x,v = self.read_inputs()
        self.a.scatter(x, v, color='red')

        self.a.set_title ("Scatter Plot)", fontsize=16)
        self.a.set_ylabel("Y", fontsize=14)
        self.a.set_xlabel("X", fontsize=14)

        self.canvas.draw()

window = Tk()
start = mclass(window)
window.mainloop()
我所做的事情:

  • 我将所有的.grid调用更改为.pack
  • 我删除了不必要的条目
  • 我将图形的定义移动到init方法中,以便 没有生成多个图形
  • 我删除了“全部”按钮,但删除了“完成和绘图”按钮。打印前读取这些值
  • 输入是字符串,必须在打印前转换为浮点列表(或np.数组)。代码现在假定逗号分隔的浮点作为x和y条目中的输入

非常感谢,非常感谢!!