我用tkinter创建了一个程序,它从树视图中获取值,将它们放入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)
答案 0 :(得分:0)
函数linspace
不是排序函数。它在指定的时间间隔内返回均匀间隔的数字(请参阅documentation)。解决问题的最简单方法是:
以下代码应该可以解决问题(未经测试,因为我没有tree
个对象)
# 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)
一些评论: