Python数据科学入门(seaborn)笔记05

Python数据科学入门笔记05——seaborn

seaborn 是matplotlib的扩展

一、seaborn 实现直方图和密度图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series,DataFrame

import seaborn as sns
s1 = Series(np.random.randn(1000))
# distplot() 可以同时绘制多个图,图名称=True
# 默认绘制hist和kde  rug 显示分布密集度  更多参数看文档
sns.distplot(s1,hist=False,kde=True,rug=True)

图一

# 密度图 
# shade 是否填充
# kdeplot() /  rugplot() 等
sns.kdeplot(s1,shade=True,color='r')

图二

# 直接调用 matplotlib 的api
# 暂时未解决问题  
sns.plt.hist(s1)

二、实现柱状图和热力图

# 下载  seaborn的数据作为实验数据
# seaborn 在github上有
df = sns.load_dataset('flights')   
df.head()
yearmonthpassengers
01949January112
11949February118
21949March132
31949April129
41949May121
df.shape
(144, 3)
# 透视表 查看数据更方便
df = df.pivot(index='month',columns='year',values='passengers')
df.head()
year194919501951195219531954195519561957195819591960
month
January112115145171196204242284315340360417
February118126150180196188233277301318342391
March132141178193236235267317356362406419
April129135163181235227269313348348396461
May121125172183229234270318355363420472
# 热力图
sns.heatmap(df)

图三

# 柱状图
s = df.sum()
sns.barplot(x=s.index,y=s.values)

图六

三、seaborn设置图像效果

1.set_style() 风格设置
x = np.linspace(0,14,100)

y1 = np.sin(x)
y2 = np.sin(x+2)*1.25
def sinplot():
    plt.plot(x,y1)
    plt.plot(x,y2)
import seaborn as sns
# 设置风格 style : dict, None, or one of {darkgrid, whitegrid, dark, white, ticks}
# 设置风格  通过字典形式修改原有参数 
sns.set_style("whitegrid",{'grid.color':'red'})  
#  上面导入 seabron 后改变了图像的效果
sinplot()

图四

# 查看风格参数 可自行设置
sns.axes_style()
{'axes.facecolor': 'white',
 'axes.edgecolor': '.8',
 'axes.grid': True,
 'axes.axisbelow': True,
 'axes.linewidth': 1.0,
 'axes.labelcolor': '.15',
 'figure.facecolor': 'white',
 'grid.color': 'red',
 'grid.linestyle': '-',
 'text.color': '.15',
 'xtick.color': '.15',
 'ytick.color': '.15',
 'xtick.direction': 'out',
 'ytick.direction': 'out',
 'xtick.major.size': 0.0,
 'ytick.major.size': 0.0,
 'xtick.minor.size': 0.0,
 'ytick.minor.size': 0.0,
 'legend.frameon': False,
 'legend.numpoints': 1,
 'legend.scatterpoints': 1,
 'lines.solid_capstyle': 'round',
 'image.cmap': 'rocket',
 'font.family': ['sans-serif'],
 'font.sans-serif': ['Arial',
  'DejaVu Sans',
  'Liberation Sans',
  'Bitstream Vera Sans',
  'sans-serif']}
# 还原默认设置
sns.set()
sinplot()

图五

2.更改曲线属性 plotting_context() 和 set_context()
# seaborn 设置的 几种context
context = ['paper','notebook','talk','poster']
# rc={} 修改原有参数
sns.set_context(context[2],rc = {'grid.linewidth':3})
sinplot()

这里写图片描述

# 查看当前 context 参数
sns.plotting_context()
{'font.size': 15.600000000000001,
 'axes.labelsize': 14.3,
 'axes.titlesize': 15.600000000000001,
 'xtick.labelsize': 13.0,
 'ytick.labelsize': 13.0,
 'legend.fontsize': 13.0,
 'grid.linewidth': 3.0,
 'lines.linewidth': 2.275,
 'patch.linewidth': 0.39,
 'lines.markersize': 9.1,
 'lines.markeredgewidth': 0.0,
 'xtick.major.width': 1.3,
 'ytick.major.width': 1.3,
 'xtick.minor.width': 0.65,
 'ytick.minor.width': 0.65,
 'xtick.major.pad': 9.1,
 'ytick.major.pad': 9.1}

四、seaborn的调色功能

def sinplot2():
    x = np.linspace(0,14,100)
    plt.figure(figsize=(8,6))  # 设置画布大小
    for i in range(4):
        plt.plot(x,np.sin(x+i)*(i+0.75),label='sin(x+%s)*(%s+0.75)'%(i,i))
        plt.legend()
sinplot2()

这里写图片描述

# 导入 seaborn 修饰图像
import seaborn as sns
sns.set_style(style='darkgrid')
sinplot2()

这里写图片描述

调色

sns.color_palette()     # RGB 颜色取值
[(0.12156862745098039, 0.4666666666666667, 0.7058823529411765),
 (1.0, 0.4980392156862745, 0.054901960784313725),
 (0.17254901960784313, 0.6274509803921569, 0.17254901960784313),
 (0.8392156862745098, 0.15294117647058825, 0.1568627450980392),
 (0.5803921568627451, 0.403921568627451, 0.7411764705882353),
 (0.5490196078431373, 0.33725490196078434, 0.29411764705882354),
 (0.8901960784313725, 0.4666666666666667, 0.7607843137254902),
 (0.4980392156862745, 0.4980392156862745, 0.4980392156862745),
 (0.7372549019607844, 0.7411764705882353, 0.13333333333333333),
 (0.09019607843137255, 0.7450980392156863, 0.8117647058823529)]
# 绘制 color_palette() 颜色板
sns.palplot(sns.color_palette())

这里写图片描述

# coclor_palette 里定义的颜色名
pal_style = ['deep', 'muted', 'bright', 'pastel', 'dark', 'colorblind']
sns.palplot(sns.color_palette('bright'))

这里写图片描述

设置调色板

sns.set_palette(sns.color_palette('bright'))
# 修改颜色版 即颜色组合后后绘制的图像
sinplot2()

这里写图片描述

# 恢复默认风格
sns.set() 
# 使用 with 语句 ,在with 语句中画图会使用其设置的风格
# 在 with 外使用默认风格
# 也就是说 with 临时设置风格
with sns.color_palette('dark'):
    sinplot2()

tu3

# 传入数值 设置自己的画板

# RGB 值
pal = sns.color_palette([(0.5,0.2,0.4),(0.3,0.9,0.2)])  
sns.palplot(pal)

tuer

sns.palplot(sns.color_palette('hls',8))

tuyi

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值