[英]How can I in matplotlib make graph from list of numbers?
I am new in programing.我是编程新手。 Could somebody hepl me with creating graph from list of numbers?有人可以帮我从数字列表中创建图表吗? To be more specific: read numbers from list and use them in y=x1*2 to create graph.更具体地说:从列表中读取数字并在 y=x1*2 中使用它们来创建图形。 Thank you for answers.谢谢你的回答。
import matplotlib.pyplot as plt
import numpy as np
import tkinter
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
root=Tk()
fig=Figure(figsize=(5,4), dpi=100)
class Win1:
def start1():
window1=tkinter.Toplevel()
Label(window1, text="Zadej hodnoty x, pro které chceš vypočítat").grid(row=0)
global e1
e1=Entry(window1)
e1.grid(row=0, column=1)
fw=tkinter.Button(window1, text="vykresli",command=Win1.vykreli1)
fw.grid(row=0,column=2)
def vykreli1():
get1=e1.get()
x1=list(get1) #I made list of numbers from Entry
y=x1*2
plt.plot(x1,y)
plt.show()
pica=Win1
f1=tkinter.Button(root, text="try1", command=pica.start1)
f2=tkinter.Button(root, text="try2")
f1.pack()
f2.pack()
root.mainloop()
Hello there and welcome at stackoverflow.你好,欢迎来到stackoverflow。
You are faced with the following challenge:您面临以下挑战:
The user enters some text.用户输入一些文本。 For example 5, 6, 12, 5.3
.例如5, 6, 12, 5.3
。 Your program reads this text in the line get1 = e1.get()
.您的程序在get1 = e1.get()
行中读取此文本。 Now the variable get1
becomes the string "5, 6, 12, 5.3"
.现在变量get1
变成了字符串"5, 6, 12, 5.3"
。 But you need a list!但是你需要一个清单! You want to have a variable that equals [5, 6, 12, 5.3]
.您希望有一个等于[5, 6, 12, 5.3]
的变量。
This means you have to parse the string get1
into a list of numbers.这意味着您必须将字符串get1
解析为数字列表。 This is a very complex task: But there are good news: Python can do it for you!这是一项非常复杂的任务:但有个好消息:Python 可以为您完成!
Import the ast
module.导入ast
模块。 Use the function ast.literal_eval
.使用 function ast.literal_eval
。
You can apply it like this:您可以像这样应用它:
x1 = ast.literal_eval(get1)
x1 = list(x1)
The second line x1 = list(x1)
makes sure that x1
is really a list.第二行x1 = list(x1)
确保x1
确实是一个列表。 Because it is possible that the user enters just "5" and not "5,6".因为用户可能只输入“5”而不是“5,6”。
Now we can calculate and plot the y values like this:现在我们可以像这样计算 plot y 值:
ys = [x * 2 for x in x1]
plt.plot(xs, ys, marker=".")
It is also possible that the user makes other mistakes.用户也可能犯其他错误。 He can enter something stupid like "5,hello,,7.8".他可以输入一些愚蠢的东西,比如“5,hello,,7.8”。
So we need some exception handling.所以我们需要一些异常处理。 I did this for you in the following example.我在下面的例子中为你做了这个。 I also renamed a few of your variables.我还重命名了一些变量。 And I am using some print
statements to make it easier to understand what is going on.我正在使用一些print
语句来更容易理解正在发生的事情。
import ast
from tkinter import messagebox
import matplotlib.pyplot as plt
import numpy as np
import tkinter
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
root = Tk()
fig = Figure(figsize=(5, 4), dpi=100)
class Win1:
def start1():
window1 = tkinter.Toplevel()
Label(window1, text="Zadej hodnoty x, pro které chceš vypočítat").grid(row=0)
global e1
e1 = Entry(window1)
e1.grid(row=0, column=1)
fw = tkinter.Button(window1, text="vykresli", command=Win1.vykreli1)
fw.grid(row=0, column=2)
def vykreli1():
user_input = e1.get()
print(f"user entered '{user_input}'")
if not user_input.strip():
messagebox.showerror("Missing entry", "You did not enter any text")
return
try:
processed_user_input = ast.literal_eval(user_input)
print(f"processed_user_input: {processed_user_input}")
xs = list(processed_user_input)
except Exception as e:
print(f"parsing error {e}")
messagebox.showerror("Unparsable", "Please enter a list of numbers like 5,6.0,20,50.2")
return
ys = [x * 2 for x in xs]
plt.plot(xs, ys, marker=".")
plt.show()
if __name__ == '__main__':
pica = Win1
f1 = tkinter.Button(root, text="try1", command=pica.start1)
f2 = tkinter.Button(root, text="try2")
f1.pack()
f2.pack()
root.mainloop()
I was able to start this program.我能够启动这个程序。 I could enter a few numbers and then I saw a graph.我可以输入几个数字,然后我看到了一个图表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.