Python 制作pyplot滑块时出错:';元组';对象没有属性';名称';
我正在尝试使用matplotlib从数据帧打印表。该表显示得非常完美,但行太多,无法一次全部显示。我试图将matplotlib滑块用作垂直滚动条,但在尝试创建滑块并将其指定给变量时,我一直遇到一个错误,我不理解。错误如下: “tuple”对象没有属性“name” 如果有人能帮我解决这个问题,我会非常感激。我已经在这上面呆了两天了,我觉得这绝对是一个简单的东西,我只是错过了,因为我已经在同一个代码上工作太久了。如果你需要更多的信息,也请告诉我 我要说的是,这个代码是用来做家庭作业的然而,任务是执行聚集聚类,而不是如何使用滚动条打印表格。知道你不仅仅是给我任务的答案,你就可以放心了。我只需要这个额外的工具来帮助理解我的结果 我的代码:Python 制作pyplot滑块时出错:';元组';对象没有属性';名称';,python,matplotlib,Python,Matplotlib,我正在尝试使用matplotlib从数据帧打印表。该表显示得非常完美,但行太多,无法一次全部显示。我试图将matplotlib滑块用作垂直滚动条,但在尝试创建滑块并将其指定给变量时,我一直遇到一个错误,我不理解。错误如下: “tuple”对象没有属性“name” 如果有人能帮我解决这个问题,我会非常感激。我已经在这上面呆了两天了,我觉得这绝对是一个简单的东西,我只是错过了,因为我已经在同一个代码上工作太久了。如果你需要更多的信息,也请告诉我 我要说的是,这个代码是用来做家庭作业的然而,任务是执行
def create_table():
n= input.get()
print(n)
model = AgglomerativeClustering(n_clusters=n, affinity='euclidean', linkage='ward')
predictions = model.fit_predict(df)
print(df)
dfArray = df.to_numpy(copy=True)
predTable = np.full((len(predictions),9),0.1)
# prints each row of data with its designated cluster
for x in range(len(predictions)):
predTable[x][0] = predictions[x]
for y in range(0,8):
#print(dfArray[x][y])
predTable[x][y+1] = dfArray[x][y]
print(dfArray[x][y])
print(predTable[x][y+1])
print(predTable)
predTableDF = pd.DataFrame(predTable)
fig, ax = plt.subplots()
ax.axis('off')
ax.axis('tight')
tableObject = ax.table(cellText=predTableDF.values,loc='center')
fig.tight_layout()
# Slider code
startScrollVal = 1
axcolor = 'lightgoldenrodyellow'
axScroll = plt.axis([0.1, 0.25, 0.0225, 0.63])
###getting error here###
slidebar = Slider(axScroll,'label',1,100,valinit=startScrollVal,orientation='vertical')
def update(val):
height = slidebar.val
tableObject.set_xdata(height/len(tableObject))
fig.canvas.draw_idle()
slider.on_changed(update)
plt.show()
这里是堆栈跟踪:
'tuple' object has no attribute 'name'
Stack trace:
> File "C:\Users\owner\source\repos\CIS362HW4\CIS362HW4\CIS362HW4.py", line 92, in create_table (Current frame)
> slidebar = Slider(axScroll,'label',1,100,valinit=startScrollVal,orientation='vertical')
> File "C:\Users\owner\source\repos\CIS362HW4\CIS362HW4\CIS362HW4.py", line 176, in <module>
> top.mainloop()
Loaded 'matplotlib.widgets'
Loaded '__main__'
Loaded 'tkinter'
Loaded 'runpy'
“tuple”对象没有属性“name”
堆栈跟踪:
>文件“C:\Users\owner\source\repos\CIS362HW4\CIS362HW4\CIS362HW4.py”,第92行,在create_表格(当前帧)中
>slidebar=滑块(axScroll,'label',1100,valinit=startScrollVal,orientation='vertical')
>文件“C:\Users\owner\source\repos\CIS362HW4\CIS362HW4\CIS362HW4.py”,第176行,在
>top.mainloop()
已加载“matplotlib.widgets”
已加载“\uuuu main\uuuuuu”
加载的“tkinter”
已加载“runpy”
我认为前一行代码应该包含.axis()
,而不是.axis()
——这些方法完全不同。@jasonharper非常感谢您!这修正了错误。我知道这一定是我忽略的简单的事情。