简体   繁体   English

ValueError: x 和 y 必须具有相同的第一维,但有形状,tkinter 和 matplotlib 问题

[英]ValueError: x and y must have same first dimension, but have shapes, tkinter and matplotlib problem

I've been playing with GUI and matlib for few days now, but I can't find an issue in the code below.几天来我一直在玩 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()

I've been getting this error:我一直收到这个错误:

ValueError: x and y must have same first dimension, but have shapes (10,) and (1,)

I tried to delete np.array我试图删除 np.array

x = np.linspace(10,1,10)
y = entry1.get()

but I don't know how can I combine y = entry1.get() with x = np.linspace(10,1,10)但我不知道如何将 y = entry1.get() 与 x = np.linspace(10,1,10) 结合起来

The function I am trying to enter is 200-4*x我要输入的 function 是200-4*x

EDIT: Full error code:编辑:完整的错误代码:

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() is not enough to convert string to function expression, to say the least.至少可以说, y = entry1.get()不足以将字符串转换为 function 表达式。 As an option try to use SymPy library with its parse_expr and lambdify methods:作为一个选项,尝试使用SymPy库及其parse_exprlambdify方法:

import tkinter as tk
from sympy import symbols, lambdify
from sympy.parsing.sympy_parser import parse_expr
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
import numpy as np

root = tk.Tk()
root.geometry('700x700')
entry1 = tk.Entry(root, width=20)
entry1.pack()


def draw():
    expr = parse_expr(entry1.get())
    lam = lambdify(symbols('x'), expr)
    x = np.array(np.linspace(-10, 10, 100))
    y = lam(x)
    plot.plot(x, y)
    canvas.draw()


tk.Button(root, text="Draw function", command=draw).pack()

fig = Figure(figsize=(5, 4), dpi=100)
plot = fig.add_subplot()
plot.spines['left'].set_position('center')
plot.spines['bottom'].set_position('zero')
plot.spines['right'].set_color('none')
plot.spines['top'].set_color('none')
canvas = FigureCanvasTkAgg(fig, master=root)

toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.update()
toolbar.pack(side=tk.BOTTOM, fill=tk.X)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)' - Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)' Matplotlib 中的 Plot K-Means:ValueError:x 和 y 必须具有相同的第一个维度,但具有形状 (10,) 和 (1,) - Plot K-Means in Matplotlib: ValueError: x and y must have same first dimension, but have shapes (10,) and (1,) Matplotlib:ValueError:x 和 y 必须具有相同的第一维 - Matplotlib: ValueError: x and y must have same first dimension Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,) - ValueError: x and y must have same first dimension, but have shapes (101,) and (100,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (224, 224, 3) - ValueError: x and y must have same first dimension, but have shapes (1,) and (224, 224, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM