我正在尝试从浮动列表中绘制散点图。我生成一个从1到len(mydata)的列表作为x值,而我的列表作为y值,这是错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\pasca\OneDrive\Dokumente\Privat\Python lernen\nested\Auswertung2.py", line 131, in <lambda>
command= lambda : draw_figure())
File "C:\Users\pasca\OneDrive\Dokumente\Privat\Python lernen\nested\Auswertung2.py", line 147, in draw_figure
self.a.scatter(data_t,y, color= 'red')
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\__init__.py", line 1601, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\axes\_axes.py", line 4498, in scatter
alpha=alpha
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\collections.py", line 883, in __init__
Collection.__init__(self, **kwargs)
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\collections.py", line 151, in __init__
offsets = np.asanyarray(offsets, float)
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core\_asarray.py", line 138, in asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
ValueError: could not convert string to float:
该守则的重要部分仅是:
class Grenzwahl (tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.label=tk.Label(self, text='Page 2', font=LARGE_FONT)
self.label.grid(row=0, column=1, padx=10, pady=10 )
self.button1 = tk.Button(self, text= 'Aktualisiere Scatter Plot',
command= lambda : draw_figure())
self.button1.grid(row=1, column=1, padx=10, pady=10)
def draw_figure():
global data_t
y=[]
for i in range(len(data_t)):
y=y + [float(i)]
self.fig = Figure(figsize=(6, 6))
self.a = self.fig.add_subplot(111)
self.a.scatter(data_t,y, color= 'red')
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.get_tk_widget().grid(row= 0, column=2, padx=10, pady=10)
self.canvas.draw()
data_t是浮点数列表,y产生为浮点数列表。我没有看到错误在哪里?
如果您想尝试整个代码,请执行以下操作:
'''
Created on 19.09.2019
@author: pasca
'''
import xlrd
import numpy as np
import pandas as pd
import tkinter as tk
from tkinter import ttk, StringVar
from tkinter.filedialog import askopenfilename
import numpy.random.common
import numpy.random.bounded_integers
import numpy.random.entropy
import xlsxwriter
from numpy.testing._private.parameterized import param
from Grid import column
from multiprocessing.managers import State
from tkinter.constants import DISABLED
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
Parameter = ['','Nach_Egelsbach', 'Nach_Chemnitz', 'Batch', 'Versuch', 'Kennwort', 'Datum', 'FLG','','','','', 'Zwick_Streifengewicht', 'Zellstoff', 'Mischung', 'TF1E', 'v', 'Lagenzahl', 'Q', 'Grund', 'Mittel', 'Decke', 'Temp_SW', 'STD', 'Produktion', 'Winkel', 'Spalt', 'PSTA', 'Ratio1', 'Ratio2', 'Vakuum_SW1','Vak_WRLP', 'Mohno_1', 'Duese_1', 'Einduesung_1', 'Mohno_2', 'Duese_2', 'Einduesung_2', 'Mohno_3', 'Duese_3', 'Einduesung_3', 'dMDmax_feucht', 'e_FmaxMD_feucht', 'dCDmax_feucht', 'e_FmaxCD_feucht', 'L_Q_feucht', 'Feuchte', 'Wassergehalt', 'Druck_Balken1', 'd_Duese1', 'hpi1', 'V1', 'P1', 'Spez_Strahlenergie1', 'Druck_Balken2', 'd_Duese2', 'hpi2', 'V2', 'P2', 'Spez_Strahlenergie2', 'Druck_Balken3', 'd_Duese3', 'hpi3', 'V3', 'P3', 'Spez_Strahlenergie3', 'Spez_Strahlenergie_ges', 'Spez_Festigkeit_Feucht', 'dMDmax_trocken', 'e_FmaxMD_trocken', 'dCDmax_trocken', 'e_FmaxCD_trocken', 'L_Q_trocken', 'Spez_Festigkeit_trocken', 'dMDmax_200', 'e_FmaxMD_200', 'dCDmax_200', 'e_FmaxCD_200', 'L_Q_200', 'Spez_Festigkeit_200', 'Dicke_trocken', 'Dicke_trocken_STABW', 'Dicke_trocken_MAX', 'Dicke_trocken_MIN', 'Dicke_nass', 'Dicke_nass_STABW', 'Dicke_nass_MAX', 'Dicke_nass_MIN', 'Fix1', 'Fix2', 'RS1', 'RS2', 'RS3', 'RS4', 'RS5', 'Pickup', 'Vakuum_WRLP3', 'Hydro_Flex_kN', 'Hydro_Flex_mbar', 'TG_vor_AquaJet', 'TG_vor_Pick_Up', 'TG_Aufrollung', 'P_IR_Trockner', 'Fasername1', 'Faserfeinheit1', 'Faserlaenge1', 'Faseranteil1', 'Faserlage1', 'Fasername2', 'Faserfeinheit2', 'Faserlaenge2', 'Faseranteil2', 'Faserlage2', 'Fasername3', 'Faserfeinheit3', 'Faserlaenge3', 'Faseranteil3', 'Faserlage3', 'Fasername4', 'Faserfeinheit4', 'Faserlaenge4', 'Faseranteil4', 'Faserlage4', 'Fasername5', 'Faserfeinheit5', 'Faserlaenge5', 'Faseranteil5', 'Faserlage5', 'Fasername6', 'Faserfeinheit6', 'Faserlaenge6', 'Faseranteil6', 'Faserlage6', 'Fasername7', 'Faserfeinheit7', 'Faserlaenge7', 'Faseranteil7', 'Faserlage7','', 'Diffusor', 'Formiersieb_Typ', 'Formiersieb_Nummer', 'Pickup_Typ', 'Pickup_Nummer', 'Hydroflex_Typ', 'Hydroflex_NR', 'Transfer_Typ', 'Transfer_NR','Zusatz', 'Flush_K1', 'Flush_K2', 'Flush_K3', 'Flush_K4', 'Flush_K5', 'Bemerkungen']
data=[]
data_t = []
LARGE_FONT =('Verdana',12)
class Auswertung(tk.Tk):
def __init__(self,*args, **kwargs):
tk.Tk.__init__(self,*args,**kwargs)
container = tk.Frame(self)
container.grid(row=0, column=1, padx=10, pady=10)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (Datenwahl,Parameterwahl,Grenzwahl):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(Datenwahl)
def show_frame(self,cont):
frame = self.frames [cont]
frame.tkraise()
def enable_button2(self):
self.frames[Datenwahl].button2.config(state = 'normal')
def extract_data(self):
#-------Exportieren der Excel File --------
global data
filename = askopenfilename()
print (filename)
workbook = xlrd.open_workbook(filename)
sheet =workbook.sheet_by_index(0)
print(type(sheet))
data = [[sheet.cell_value(r+5,c) for c in range(sheet.ncols-5) ] for r in range(sheet.nrows-7)]
print (data)
return data
def get_column(self, parameter):
global data_t
Values =[]
global Parameter
for i in range(len(Parameter)):
Values=Values + [i]
param_dict = dict(zip(Parameter,Values))
sort_param= int(param_dict[parameter])
data_t=[i[sort_param] for i in data]
print(data_t)
return data_t
def qf(stringtoprint):
print(stringtoprint)
class Datenwahl(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent,)
self.label=tk.Label(self, text='Start Page', font=LARGE_FONT)
self.label.grid(row=0, column=1, padx=10, pady=10 ,sticky= 'ew')
self.button1 = tk.Button(self, text= 'Daten Einlesen',
command= lambda : [controller.enable_button2(), controller.extract_data()])
self.button1.grid(row=1, column=1, padx=10, pady=10)
self.button2 = tk.Button(self, text= 'Zur Parameter Wahl', state='disabled',
command= lambda : controller.show_frame(Parameterwahl))
self.button2.grid(row=2, column=1, padx=10, pady=10)
class Parameterwahl(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
global Parameter
self.box_value=StringVar()
self.label=tk.Label(self, text='Page one', font=LARGE_FONT)
self.label.grid(row=0, column=1, padx=10, pady=10)
self.ddMenu = ttk.Combobox(self,textvariable = self.box_value ,values= Parameter
)
self.ddMenu.grid(row=1, column=1, padx=10, pady=10)
self.button1 = tk.Button(self, text= 'Wähle deine Grenzen',
command= lambda : [controller.get_column(self.ddMenu.get()), controller.show_frame(Grenzwahl)])
self.button1.grid(row=2, column=1, padx=10, pady=10)
class Grenzwahl (tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.label=tk.Label(self, text='Page 2', font=LARGE_FONT)
self.label.grid(row=0, column=1, padx=10, pady=10 )
self.button1 = tk.Button(self, text= 'Aktualisiere Scatter Plot',
command= lambda : draw_figure(self))
self.button1.grid(row=1, column=1, padx=10, pady=10)
def draw_figure(self):
global data_t
y=[]
for i in range(len(data_t)):
y=y + [float(i)]
self.fig = Figure(figsize=(6, 6))
self.a = self.fig.add_subplot(111)
self.a.scatter(data_t,y, color= 'red')
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.get_tk_widget().grid(row= 0, column=2, padx=10, pady=10)
self.canvas.draw()
app = Auswertung()
app.mainloop()