之前分享过pandas也是可以作图的,今天复习一下pandas作图,并与seaborn做对比,熟悉下各自绘图的特点。
也可以用seaborn作图,代码如下
我们发现大多数用户店家评分都是4分及以上
从上面两个例子看,pandas和seaborn绘图各有千秋,有时候pandas简洁,有时候seaborn简洁。
导入用到的库
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.style.use('fivethirtyeight')
plt.style.use('bmh')
读取数据
business = pd.read_csv('data/business.csv')
business.head()
![f8462b71c956502a0e2189a312969963.png](https://i-blog.csdnimg.cn/blog_migrate/4efadf970925a28965665ce8acfa92ff.jpeg)
评分分布
查看用户对yelp平台内店家评价的分布情况。 使用pandas绘图
colors = sns.color_palette()
#stars是series数据类型
stars = business['stars'].value_counts().sort_index()
stars.plot(kind='bar',
figsize=(10, 5),
color=colors[:9],
rot=0,
title='Distribution of rating')
![ec4e1d196b81a0441a753bda648a99b9.png](https://i-blog.csdnimg.cn/blog_migrate/e587c6304326f5307a684de68e673e08.png)
plt.figure(figsize=(10,5))
sns.countplot(business['stars'])
plt.title('Distribution of rating')
![316a678d6a67e5f05153b1dcaab776d7.png](https://i-blog.csdnimg.cn/blog_migrate/44d4df81ee14fd2093628c5ef783d4a6.png)
最常见的店名和坐标
我们看看店铺的最常见的店名、最常见的坐标。 使用pandas绘图
fig, ax = plt.subplots(1, 2, figsize=(14, 8))
business['name'].value_counts()[:20].plot(kind='barh',
ax=ax[0],
color=colors[:20],
title='Top 20 name of store in Yelp')
business['city'].value_counts()[:20].plot(kind='barh',
ax=ax[1],
color=colors[:20],
title='Top 20 of city in Yelp')
![146344dc9259cc495827dfd88c8f898a.png](https://i-blog.csdnimg.cn/blog_migrate/49bdf647242f1af766231fa4485b5bdf.png)
f,ax = plt.subplots(1,2, figsize=(14,8))
cnt = business['name'].value_counts()[:20].to_frame()
sns.barplot(cnt['name'], cnt.index, palette = 'RdBu', ax = ax[0])
ax[0].set_xlabel('')
ax[0].set_title('Top 20 name of store in Yelp')
cnt = business['city'].value_counts()[:20].to_frame()
sns.barplot(cnt['city'], cnt.index, palette = 'rainbow', ax =ax[1])
ax[1].set_xlabel('')
ax[1].set_title('Top 20 of city in Yelp')
plt.subplots_adjust(wspace=0.3)
![a4798d83e0b0c1f0ebae42b611c54d11.png](https://i-blog.csdnimg.cn/blog_migrate/a8a8b24194c7a80a09e83c7311e90b6e.png)
-END-
假期无法出门不如在家学习
![2890409d6234166100455be790b9a01e.png](https://i-blog.csdnimg.cn/blog_migrate/6e5d3006c97b884f8ca0ee22da3c1afd.jpeg)
世界正在奖励坚持学习的人!