我用tkinter编写了一个程序,它从treeview获取值,将它们放入numpy数组列表(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
不是排序函数。它返回指定间隔内的等距数字(请参见documentation)。解决问题的最简单方法是:下面的代码应该可以做到这一点(未经测试,因为我没有
tree
对象)一些评论:
相关问题 更多 >
编程相关推荐