tkinter Python上使用matplotlib绘制线时出错
我用tkinter制作了一个程序,从treeview中获取值,将它们放入numpy数组列表(tkinter Python上使用matplotlib绘制线时出错,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我用tkinter制作了一个程序,从treeview中获取值,将它们放入numpy数组列表(Xprojekti,Yprojekti)中,然后在tkinter窗口上绘制图形。 这两个列表中的值不是按顺序排列的,但我想打印它们,以便轴上的值按顺序排列。当我尝试这样做时,会出现以下错误: for seg in self._plot_args(this, kwargs): File "C:\Python36-32\lib\site-packages\matplotlib\axes\_base
Xprojekti,Yprojekti
)中,然后在tkinter窗口上绘制图形。
这两个列表中的值不是按顺序排列的,但我想打印它们,以便轴上的值按顺序排列。当我尝试这样做时,会出现以下错误:
for seg in self._plot_args(this, kwargs):
File "C:\Python36-32\lib\site-packages\matplotlib\axes\_base.py", line 384, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python36-32\lib\site-packages\matplotlib\axes\_base.py", line 243, in _xy_from_xy
"have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (50,) and (3,)
我该怎么办
Xprojekti=np.array([])
Yprojekti=np.array([])
for child in tree.get_children():
ProjectSize=round(float(tree.item(child,"values")[1]),2)
Xprojekti=np.append(Xprojekti, ProjectSize)
for child in tree.get_children():
ProjectCost=round(float(tree.item(child,"values")[3]),2)
Yprojekti=np.append(Yprojekti, ProjectCost)
X=np.linspace(np.min(Xprojekti), np.max(Xprojekti)) # I used this to sort the values on X
fprojekti=Figure(figsize=(6.5, 4.2),dpi=83)
grafikprojekti=fprojekti.add_subplot(111)
grafikprojekti.plot(X, Yprojekti, color="blue",marker="o", linewidth=1)
grafikprojekti.set_xlabel("Size")
grafikprojekti.set_ylabel("Costs")
grafikprojekti.set_title("All projects")
grafikprojekti.grid()
canvasprojects=FigureCanvasTkAgg(fprojekti, master=Database)
canvasprojects.show()
canvasprojects.get_tk_widget().grid(row=1,column=2,sticky="wn",padx=5)
toolbar_frameprojekti=Frame(Database)
toolbar_frameprojekti.grid(row=2,column=2,sticky="wn",padx=5)
toolbarprojects = NavigationToolbar2TkAgg(canvasprojects,toolbar_frameprojekti)
toolbarprojects.update()
canvasprojects._tkcanvas.grid(row=1,column=2,sticky="wn",padx=5,pady=10)
函数
linspace
不是排序函数。它返回指定间隔内的等距数字(请参见)。解决此问题的最简单方法是:
树
对象)
几点意见:
你好包含完整的堆栈跟踪或完整的自包含示例都会有所帮助。给出错误消息后,我假设错误在
grafikprojekti.plot(X,Yprojekti,color=“blue”,marker=“o”,linewidth=1)行上
。是吗?我想是这样的。我试图删除这一行(X=np.linspace(np.min(Xprojekti),np.max(Xprojekti))
),并绘制Xprojekti和Yprojekti。然后它工作了,但是X值不符合顺序,这行代码遍历了整个图形请阅读和理解。我需要Xprojekti中第一位的值对应于Yprojekti中第一个值的值,Xprojekti中第二个值对应于Yprojekti中第二个值,依此类推。我给出的代码应该做到这一点。你测试过了吗?结果如何?
# Get a list of points
points = []
for child in tree.get_children():
points.append((float(tree.item(child,"values")[1]),
float(tree.item(child,"values")[3])))
# Sort the points
points = sorted(point)
# Extract X and Y
X = [v[0] for v in points]
Y = [v[1] for v in points]
# And now the rest of our code
projekti=Figure(figsize=(6.5, 4.2),dpi=83)
grafikprojekti=fprojekti.add_subplot(111)
grafikprojekti.plot(X, Y, color="blue",marker="o", linewidth=1)