Python数据可视化—seaborn简介和实例

Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图。这里实例采用的数据集都是seaborn提供的几个经典数据集,dataset文件可见于Github。本博客只总结了一些,方便博主自己查询,详细介绍可以看seaborn官方APIexample gallery,官方文档还是写的很好的。

1  set_style( )  set( )

set_style( )是用来设置主题的,Seaborn有五个预设好的主题: darkgrid , whitegrid , dark , white ,和 ticks  默认: darkgrid

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")
plt.plot(np.arange(10))
plt.show()



set( )通过设置参数可以用来设置背景,调色板等,更加常用。

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white", palette="muted", color_codes=True)     #set( )设置主题,调色板更常用
plt.plot(np.arange(10))
plt.show()

2  distplot( )  kdeplot( )

distplot( )为hist加强版,kdeplot( )为密度曲线图 
import matplotlib.pyplot as plt
import seaborn as sns
df_iris = pd.read_csv('../input/iris.csv')
fig, axes = plt.subplots(1,2)
sns.distplot(df_iris['petal length'], ax = axes[0], kde = True, rug = True)        # kde 密度曲线  rug 边际毛毯
sns.kdeplot(df_iris['petal length'], ax = axes[1], shade=True)                     # shade  阴影                       
plt.show()
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set( palette="muted", color_codes=True)
rs = np.random.RandomState(10)
d = rs.normal(size=100)
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot(d, kde=False, color="b", ax=axes[0, 0])
sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])
sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])
sns.distplot(d, color="m", ax=axes[1, 1])
plt.show()

3  箱型图 boxplot( )

import matplotlib.pyplot as plt
import seaborn as sns
df_iris = pd.read_csv('../input/iris.csv')
sns.boxplot(x = df_iris['class'],y = df_iris['sepal width'])
plt.show()


import matplotlib.pyplot as plt
import seaborn as sns
tips = pd.read_csv('../input/tips.csv')
sns.set(style="ticks")                                     #设置主题
sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")   #palette 调色板
plt.show()

4  联合分布jointplot( )

tips = pd.read_csv('../input/tips.csv')   #右上角显示相关系数
sns.jointplot("total_bill", "tip", tips)
plt.show()

tips = pd.read_csv('../input/tips.csv')
sns.jointplot("total_bill", "tip", tips, kind='reg')     
plt.show()


5  热点图heatmap( )

import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv("../input/car_crashes.csv")
data = data.corr()
sns.heatmap(data)
plt.show()


6  pairplot( )

import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv("../input/iris.csv")
sns.set()                        #使用默认配色
sns.pairplot(data,hue="class")   #hue 选择分类列
plt.show()


import seaborn as sns
import matplotlib.pyplot as plt
iris = pd.read_csv('../input/iris.csv')
sns.pairplot(iris, vars=["sepal width", "sepal length"],hue='class',palette="husl")  
plt.show()

7  FacetGrid( )

import seaborn as sns
import matplotlib.pyplot as plt
tips = pd.read_csv('../input/tips.csv')
g = sns.FacetGrid(tips, col="time",  row="smoker")
g = g.map(plt.hist, "total_bill",  color="r")
plt.show()

参考链接:

  • 65
    点赞
  • 675
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个Python数据可视化的QQ音乐实例,使用了MatplotlibSeaborn库来绘制图表和图形。 首先,我们需要安装这两个库: ```python !pip install matplotlib seaborn ``` 然后,我们需要获取QQ音乐的数据。我们可以使用爬虫来获取网站的数据,或者使用QQ音乐提供的API。 以下是一个使用QQ音乐API来获取某个歌手的热门歌曲的例子: ```python import requests # 使用QQ音乐API获取某个歌手的热门歌曲 url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp' params = { 'ct': '24', 'qqmusic_ver': '1298', 'new_json': '1', 'remoteplace': 'sizer.yqq.song_next', 'searchid': '64405487069162918', 't': '0', 'aggr': '1', 'cr': '1', 'catZhida': '1', 'lossless': '0', 'flag_qc': '0', 'p': '1', 'n': '20', 'w': '周杰伦', 'g_tk': '5381', 'jsonpCallback': 'MusicJsonCallback', 'loginUin': '0', 'hostUin': '0', 'format': 'jsonp', 'inCharset': 'utf8', 'outCharset': 'utf-8', 'notice': '0', 'platform': 'yqq.json', 'needNewCode': '0' } headers = { 'referer': 'https://y.qq.com/n/yqq/singer/0025NhlN2yWrP4.html', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } res = requests.get(url, params=params, headers=headers) json_data = res.json() song_list = json_data['data']['song']['list'] ``` 接下来,我们可以使用MatplotlibSeaborn来绘制图表和图形。以下是一个绘制该歌手的热门歌曲排行榜的例子: ```python import matplotlib.pyplot as plt import seaborn as sns # 绘制该歌手的热门歌曲排行榜 song_names = [song['name'] for song in song_list] song_scores = [song['score'] for song in song_list] sns.set(style='whitegrid') plt.figure(figsize=(12,6)) sns.barplot(x=song_scores, y=song_names, palette='Blues_d') plt.title('周杰伦的热门歌曲排行榜') plt.xlabel('播放量') plt.ylabel('歌曲名称') plt.show() ``` 该代码会生成一个水平条形图,显示该歌手的热门歌曲排行榜,按播放量从高到低排序。我们可以使用其他的MatplotlibSeaborn功能来自定义这个图表,比如添加标签、调整颜色和字体等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值