我的代码:
import matplotlib.pyplot as plt
import pandas as pd
# set directory
df = pd.read_excel('Angle difference plot.xlsx', 'Sheet1')
# set plot
plt.plot(df['Angle'], df['RE Angle'])
# set label
plt.xlabel('calculated angle')
plt.ylabel('ground truth)')
plt.title('angle accuracy plot')
plt.legend()
plt.show
执行上述代码时,出现以下错误:
return self._engine.get_loc(key) File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Angle'
有人可以解释一下为什么我会看到此错误吗?我的excel文件看起来像这样:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9zaW54bS5wbmcifQ==” alt =“在此处输入图像描述”>
非常感谢您的帮助
pandas.Series.str.strip
pandas.Series.str.strip
import pandas as pd
import matplotlib.pyplot as plt
# set directory
df = pd.read_excel('Angle difference plot.xlsx', 'Sheet1')
# clear whitespace from the beginning and end of the column names
df.columns = df.columns.str.strip()
# plot
plt.plot('Angle', 'RE Angle', data=df)
plt.plot(df[df.columns[0]], df[df.columns[1]])
,应该是plt.show
。 此代码运行良好:
plt.show()