|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码
import pandas as pd
import matplotlib.pyplot as plt
import os
def dataVisualization():
fileName=input('请输入要打开的文件名movies_revenue_starring_1950_2010.txt:')
df_tem3=pd.read_csv(fileName,encoding='cp936')
df_tem3.sort_values(by='Title',ascending=False)#按列对数据进行降序排列
df_tem3=df_tem3.loc[:,['Revenue','Starring Actors Popularity']]
#利用matplotlib绘图
plt.figure(figsize=(12,8))
plt.plot(df_tem3['Title'],df_tem3['Revenue'],label=\
'Revenue',color='red')
plt.xlabel("Title",fontsize=12)
#姓名每隔10个抽样显示
xlength=len(df_tem3)
print('xlength=',xlength)
#构建xticks显示位置
xticksloc=[i for i in range(xlength)if i%10==0]
print('xticksloc=',xticksloc)
#构建xticks显示标签
xtickslabels=df_tem3['Title'].values[::10]
print('xtickslabels',xtickslabels)
plt.xticks(xticksloc,xtickslabels,rotation=45)#倾斜45度显示
plt.legend(fontsize=15)#显示图例并设置字号
plt.savefig('movies_revenue_1950_2010.png',dpi=400)
plt.show()
plt.figure(figsize=(12,8))
plt.plot(df_tem3['Title'],df_tem3['Starring Actors Popularity'],label=\
' Starring Actors Popularity',color='green')
plt.xlabel("Title",fontsize=12)
#姓名每隔10个抽样显示
xlength=len(df_tem3)
print('xlength=',xlength)
#构建xticks显示位置
xticksloc=[i for i in range(xlength)if i%10==0]
print('xticksloc=',xticksloc)
#构建xticks显示标签
xtickslabels=df_tem3['Title'].values[::10]
print('xtickslabels',xtickslabels)
plt.xticks(xticksloc,xtickslabels,rotation=45)#倾斜45度显示
plt.legend(fontsize=15)#显示图例并设置字号
plt.savefig('movies_starpopularity_1950_2010.png',dpi=400)
plt.show()
print('任务5执行成功!')
print(dataVisualization())
报错
Traceback (most recent call last):
File "C:\Users\阿泽\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Title'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/阿泽/AppData/Local/Programs/Python/Python39/fz1.py", line 52, in <module>
print(dataVisualization())
File "C:/Users/阿泽/AppData/Local/Programs/Python/Python39/fz1.py", line 17, in dataVisualization
plt.plot(df_tem3['Title'],df_tem3['Revenue'],label=\
File "C:\Users\阿泽\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\阿泽\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err |
|