pandas绘图_小案例对比pandas与seaborn可视化效果,谁更优秀?

之前分享过pandas也是可以作图的,今天复习一下pandas作图,并与seaborn做对比,熟悉下各自绘图的特点。

导入用到的库

import pandas as pdimport matplotlib.pyplot as pltimport seaborn as sns%matplotlib inlineplt.style.use('fivethirtyeight')plt.style.use('bmh')

读取数据

business = pd.read_csv('data/business.csv')business.head()
f8462b71c956502a0e2189a312969963.png

评分分布

查看用户对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 也可以用seaborn作图,代码如下
plt.figure(figsize=(10,5))sns.countplot(business['stars'])plt.title('Distribution of rating')
316a678d6a67e5f05153b1dcaab776d7.png 我们发现大多数用户店家评分都是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')
146344dc9259cc495827dfd88c8f898a.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 从上面两个例子看,pandas和seaborn绘图各有千秋,有时候pandas简洁,有时候seaborn简洁。

-END-

假期无法出门不如在家学习

2890409d6234166100455be790b9a01e.png

世界正在奖励坚持学习的人!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值