![b2565f5dbe38f95a30f745c04e8eb5b0.png](https://i-blog.csdnimg.cn/blog_migrate/4d41ea420efd6811e2c1606b7b4e5c78.jpeg)
Seaborn 是基于 Matplotlib 的 Python 可视化库,在 Matplotlib 的基础上做了进一步的封装,内置了很多主题,默认情况下就可以绘制出比 Matplotlib 更加赏心悦目的图表。
如图 4.1 所示,按照电影等级为类别,画出每一年电影数量的折线图,只需要使用 Seaborn 的pointplot ( ) 函数即可。
import
![cedf149d080a4185f8c3df583239449b.png](https://i-blog.csdnimg.cn/blog_migrate/a9ed9907dc1416248e99e5103726ab4f.jpeg)
使用 Seaborn 画图时,一般都要传入 data 参数,表示绘图时使用的 DataFrame 类型的数据,x 表示横轴使用的字段,y 表示纵轴使用的字段,hue 表示分组字段,用起来非常方便。
Seaborn 还可以制作柱状图、直方图、散点图、箱线图等,基本用法和 pointplot( ) 函数很类似,这里就不详细介绍了,通过几个例子来熟悉一下。
使用 countplot( ) 函数绘制电影类型的柱状图,如图 4.2 所示,注意参数 y 的值为Movie_Type,所以柱状图是横向的,如果指定 x 的值为 Movie_Type 则是纵向柱状图。
movie_pd['Movie_Type'] = movie_pd['Type'].map(lambda x: x.split(',')[0])
sns.countplot(y = 'Movie_Type', data = movie_pd)
plt.title('Movie Count By Type')
plt.tick_params(labelsize = 9)
plt.xlabel('')
plt.ylabel('')
plt.show()
![8974a40004ac86922ed498ef1c585c27.png](https://i-blog.csdnimg.cn/blog_migrate/1f53cca256be2171ee3b1fb219c908a6.jpeg)
使用 barplot( ) 绘制按照电影等级分组的柱状图。
movie_pd['Grade'] = pd.cut(movie_pd['Rating'], bins=[0, 6, 7.5, 10], labels=['Bad', 'Normal', 'Excellent'])
group_pd = movie_pd.groupby(['Year', 'Grade']).size().reset_index(name='Count')
sns.barplot(x = 'Year', y = 'Count', hue = 'Grade', data = group_pd)
plt.title('Movie Count By Year and Grade')
plt.show()
![65fe48d46b895fba2604166246403ad3.png](https://i-blog.csdnimg.cn/blog_migrate/bb0a35b33457c4591023209879410cc5.jpeg)
使用 pairplot( ) 同时绘制电影评分和投票数的直方图、散点图。
sns.pairplot(movie_pd[['Rating', 'Votes']])
plt.show()
![24fece862f954447e0cd5704db830fba.png](https://i-blog.csdnimg.cn/blog_migrate/51777e0eec5a52806f815638937e43f3.jpeg)
使用 boxplot( ) 绘制电影票房收入的箱线图。
sns.boxplot(x = 'Year', y = 'Revenue', data = movie_pd)
plt.title('Movie Revenue By Year')
plt.show()
![18b9ed2f21a34c974b681eb9dbf64ca5.png](https://i-blog.csdnimg.cn/blog_migrate/7453ef3736459bf629decbac409ffae0.jpeg)