在以下代码中添加代码,使得保存的折线图更简洁更美观: import pandas as pd import matplotlib.pyplot as plt df=pd.read_csv('movies_revenue_starring_1950_2010.txt',sep=',') df.sort_values('Title',ascending=False,inplace=True) df_new=df[['Title','Revenue']].reset_index(drop=True) df_new=df_new.iloc[::65,:] #(进行位置索引)对title进行抽样 df_renew=df[['Title','Starring Actors Popularity']].reset_index(drop=True) df_renew=df_renew.iloc[::65,:] #可视化电影的revenue的值 plt.plot(df_new['Title'],df_new['Revenue'],color='red') plt.xticks(rotation=45) #x轴旋转45° plt.title('movies Revenue 1950-2010') plt.xlabel('title') plt.ylabel('Revenue') plt.savefig('movies_revenue_1950_2010.png',dpi=400) plt.show() #可视化电影的Starring Actors Popularity值 plt.plot(df_renew['Title'],df_renew['Starring Actors Popularity'],color='green') plt.xticks(rotation=45) plt.title('Starring Actors Popularity 1950-2010') plt.xlabel('title') plt.ylabel('Starring Actors Popularity') plt.savefig('movies_starpopularity_1950_2010.png',dpi=400) plt.show()
时间: 2023-08-07 22:03:38 浏览: 78
能用的折线图
你可以尝试以下代码,使得保存的折线图更简洁更美观:
```
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv('movies_revenue_starring_1950_2010.txt',sep=',')
df.sort_values('Title',ascending=False,inplace=True)
df_new=df[['Title','Revenue']].reset_index(drop=True)
df_new=df_new.iloc[::65,:] #(进行位置索引)对title进行抽样
df_renew=df[['Title','Starring Actors Popularity']].reset_index(drop=True)
df_renew=df_renew.iloc[::65,:]
#可视化电影的revenue的值
plt.plot(df_new['Title'],df_new['Revenue'],color='red')
plt.xticks(rotation=45) #x轴旋转45°
plt.title('movies Revenue 1950-2010')
plt.xlabel('title')
plt.ylabel('Revenue')
plt.tight_layout() # 自动调整子图间的间距和间隔
plt.savefig('movies_revenue_1950_2010.png',dpi=400)
plt.show()
#可视化电影的Starring Actors Popularity值
plt.plot(df_renew['Title'],df_renew['Starring Actors Popularity'],color='green')
plt.xticks(rotation=45)
plt.title('Starring Actors Popularity 1950-2010')
plt.xlabel('title')
plt.ylabel('Starring Actors Popularity')
plt.tight_layout() # 自动调整子图间的间距和间隔
plt.savefig('movies_starpopularity_1950_2010.png',dpi=400)
plt.show()
```
使用 `plt.tight_layout()`函数可以自动调整子图间的间距和间隔,使得保存的折线图更加紧凑和美观。
阅读全文