我已经使用GUI和matlib玩了几天了,但在下面的代码中找不到问题
from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
root = Tk()
root.geometry('700x700')
entry1 = Entry(root, width = 20)
entry1.pack()
def button_command():
x = np.array(np.linspace(10,1,10))
y = np.array(entry1.get())
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(x,y, "r")
plt.show()
return None
Button(root, text="Draw function", command=button_command).pack()
root.mainloop()
我一直遇到这个错误:
ValueError: x and y must have same first dimension, but have shapes (10,) and (1,)
我试图删除np.array
x = np.linspace(10,1,10)
y = entry1.get()
但我不知道如何将y=entry1.get()与x=np.linspace(10,1,10)结合起来
我尝试输入的函数是200-4*x
编辑: 完整错误代码:
Traceback (most recent call last):
File "Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "gui.py", line 21, in button_command
plt.plot(x,y, "r")
File "Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 2988, in plot
return gca().plot(
File "Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "Python\Python39\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "Python\Python39\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (10,) and (1,)
y = entry1.get()
至少可以说不足以将字符串转换为函数表达式。作为一种选择,尝试使用SymPy库及其parse_expr和lambdify方法:相关问题 更多 >
编程相关推荐