读取文本文件movies_revenue_starring_1950_2010.txt, 首先按照电影title降序排序,然后分别可视化电影的revenue Starring Actors Popularity 的值,每幅图均包括图例、图标题,x轴刻度值为电影title且斜45°显示(为了显示美观,可以将电影title每隔若干个抽样显示),每幅图曲线颜色分别为红色、绿色;每幅图分别保存为png图片保存,分辨率为400 dpi,png图片命名分别为movies_revenue_1950_2010.png、movies_ starpopularity_ 1950 2010.png
时间: 2023-12-14 14:36:04 浏览: 102
以下是Python代码实现:
```python
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 设置中文字体
my_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc')
# 读取数据并按照电影title降序排序
df = pd.read_csv('movies_revenue_starring_1950_2010.txt', sep='\t').sort_values('Title', ascending=False)
# 对Title进行抽样,每隔10个显示一个Title
title_list = df['Title'].tolist()[::10]
# 可视化电影的revenue
fig1, ax1 = plt.subplots(figsize=(20, 10))
ax1.plot(df['Title'], df['Revenue (Millions)'], color='r', label='Revenue')
ax1.set_xticklabels(title_list, fontproperties=my_font, rotation=45)
ax1.set_xlabel('Movie Title', fontproperties=my_font)
ax1.set_ylabel('Revenue (Millions)', fontproperties=my_font)
ax1.legend(prop=my_font)
ax1.set_title('Revenue of Movies from 1950 to 2010', fontproperties=my_font)
plt.savefig('movies_revenue_1950_2010.png', dpi=400)
# 可视化电影的Starring Actors Popularity
fig2, ax2 = plt.subplots(figsize=(20, 10))
ax2.plot(df['Title'], df['Starring Actors Popularity'], color='g', label='Starring Actors Popularity')
ax2.set_xticklabels(title_list, fontproperties=my_font, rotation=45)
ax2.set_xlabel('Movie Title', fontproperties=my_font)
ax2.set_ylabel('Starring Actors Popularity', fontproperties=my_font)
ax2.legend(prop=my_font)
ax2.set_title('Starring Actors Popularity of Movies from 1950 to 2010', fontproperties=my_font)
plt.savefig('movies_starpopularity_1950_2010.png', dpi=400)
```
运行以上代码,即可得到movies_revenue_1950_2010.png和movies_starpopularity_1950_2010.png两张图片。
阅读全文