Python網絡爬蟲與文本數據分析(視頻課)
之前分享過pandas也是可以作圖的,今天複習一下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()
評分分布查看用戶對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')
也可以用seaborn作圖,代碼如下
plt.figure(figsize=(10,5))
sns.countplot(business['stars'])
plt.title('Distribution of rating')
我們發現大多數用戶店家評分都是4分及以上
最常見的店名和坐標我們看看店鋪的最常見的店名、最常見的坐標。使用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')
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)
從上面兩個例子看,pandas和seaborn繪圖各有千秋,有時候pandas簡潔,有時候seaborn簡潔。
-END-
假期無法出門
不如在家學習
世界正在獎勵堅持學習的人!