You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行绘图代码触发AttributeError: 'DataFrame'无'plt'属性,如何修正?

修正AttributeError: 'DataFrame' object has no attribute 'plt'错误

问题出在绘图代码的方法调用上:你误将pandas DataFrame的绘图方法写成了plt,但实际上DataFrame的绘图接口是plot——plt是你导入的matplotlib.pyplot模块的别名,不能直接通过DataFrame实例调用。

修正后的完整代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# loading data
file = pd.read_csv("students_scoure.csv")
# print(file.shape)
# print(file.head())
# print(file.describe())

# plot the data
file.plot(x='Hours', y='Scores', style='o')
plt.show()

修改后,DataFrame会调用内置的plot方法生成指定的散点图,最终通过plt.show()将图像显示出来。

内容的提问来源于stack exchange,提问作者Piyush Joshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:15:30