修改以下代码,使得x轴文字标签不重叠: 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.figure(figsize=(18,14)) plt.plot(df_new['Title'],df_new['Revenue'],color='red') plt.xticks(rotation=45,fontsize=7) #x轴旋转45° plt.title('movies Revenue 1950-2010',fontsize=16) plt.xlabel('title') plt.ylabel('Revenue') plt.savefig('movies_revenue_1950_2010.png',dpi=400) plt.show() #可视化电影的Starring Actors Popularity值 plt.figure(figsize=(18,14)) plt.plot(df_renew['Title'],df_renew['Starring Actors Popularity'],color='green') plt.xticks(rotation=45,fontsize=7) plt.title('Starring Actors Popularity 1950-2010',fontsize=16) plt.xlabel('title') plt.ylabel('Starring Actors Popularity
时间: 2023-08-08 21:07:35 浏览: 125
') plt.subplots_adjust(bottom=0.3) #调整子图间距 plt.savefig('Starring_Actors_Popularity_1950_2010.png',dpi=400) plt.show()
修改后的代码如下:
```
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.figure(figsize=(18,14))
plt.plot(df_new['Title'],df_new['Revenue'],color='red')
plt.xticks(rotation=45,fontsize=7, ha='right') #x轴旋转45°,右对齐
plt.title('movies Revenue 1950-2010',fontsize=16)
plt.xlabel('title')
plt.ylabel('Revenue')
plt.subplots_adjust(bottom=0.3) #调整子图间距
plt.savefig('movies_revenue_1950_2010.png',dpi=400)
plt.show()
#可视化电影的Starring Actors Popularity值
plt.figure(figsize=(18,14))
plt.plot(df_renew['Title'],df_renew['Starring Actors Popularity'],color='green')
plt.xticks(rotation=45,fontsize=7, ha='right') #x轴旋转45°,右对齐
plt.title('Starring Actors Popularity 1950-2010',fontsize=16)
plt.xlabel('title')
plt.ylabel('Starring Actors Popularity')
plt.subplots_adjust(bottom=0.3) #调整子图间距
plt.savefig('Starring_Actors_Popularity_1950_2010.png',dpi=400)
plt.show()
```
修改的内容包括:
1. 在xticks中添加ha='right'参数,使得x轴标签右对齐。
2. 在可视化图形中添加plt.subplots_adjust(bottom=0.3)调整子图间距,防止标签重叠。
阅读全文