Seaborn学习笔记总结大全

import seaborn as sns
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

import random

def sinplot(filp=1):
    x = np.linspace(0,14,100) # 0-14 生成100个点
    for i in range(1,7):
        plt.plot(x,np.sin(x + i*0.5)*(7-i)*filp)
sinplot()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X0ocxZkP-1634142896758)(output_1_0.png)]

组合曲线图

sns.set() # 设置风格或者主题
sinplot()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uM2EJILj-1634142896763)(output_3_0.png)]

风格主题一般有

  • darkgrid
  • whitegrid
  • dark
  • white
  • ticks

箱线图

np.random.seed(0) # 设置全局随机种子
sns.set_style('ticks')
# 产生一个正态分布的20*6的数组,点对点
data = np.random.normal(size = (20,6))+np.arange(6)/2

sns.boxplot(data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x2598873b888>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WHjqG4GV-1634142896767)(output_6_1.png)]

# 去掉上和右框线
sns.boxenplot(data=data)
sns.despine(offset=10)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8CGIr2FP-1634142896770)(output_7_0.png)]

sns.violinplot(data)
sns.despine(offset=10) # offset调节轴线距离

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ttyzDyfC-1634142896772)(output_8_0.png)]

sns.set_style('whitegrid')
sns.boxplot(data=data,palette='deep')
# sns.despine(left=True)
<matplotlib.axes._subplots.AxesSubplot at 0x25988609048>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MfCTkxa4-1634142896774)(output_9_1.png)]

sns.set_style('whitegrid')
sns.boxplot(data=data,palette='deep')
sns.despine(left=True) # 指定隐藏

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WXmE5OFf-1634142896777)(output_10_0.png)]

## 指定多个子图
with sns.axes_style('ticks'):
    plt.subplot(211)
    sinplot()
plt.subplot(212)
sinplot(-1)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9A9W7z9O-1634142896779)(output_11_0.png)]

sns.set()

调节线条和坐标轴大小

sns.set_context('paper') # 类似的参数还有poster\notebook
plt.figure(figsize=(8,6))
sinplot()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s1OCqVaQ-1634142896789)(output_14_0.png)]

sns.set_context('poster') # 类似的参数还有poster(要大一点)\notebook
plt.figure(figsize=(8,6))
sinplot()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T2HzXiPN-1634142896791)(output_15_0.png)]

sns.set_context('notebook',font_scale=5.5,rc={'lines.linewidth':10.5}) # font_size 坐标数字大小,rc是线条粗细
plt.figure(figsize=(8,6))
sinplot()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XyUi3rwB-1634142896792)(output_16_0.png)]

调色

调色板

  • color_palette()传入颜色
  • set_palette()设置图的颜色

分类调色板

current_palette = sns.color_palette()
sns.palplot(current_palette)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2zl5hhiO-1634142896794)(output_20_0.png)]

一共10个颜色,通过color_palette()调节个数

sns.palplot(sns.color_palette('hls',20)) # hls是颜色空间,传出20种颜色

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EGoJPLxc-1634142896795)(output_22_0.png)]

sns.set(rc={'figure.figsize':(20,8)}) # 指定画布大小
data = np.random.normal(size=(20,20))+np.arange(20)/2
sns.boxenplot(data=data,palette=sns.color_palette('hls',20))
<matplotlib.axes._subplots.AxesSubplot at 0x2598a3da108>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KakXrS9L-1634142896797)(output_23_1.png)]

控制颜色亮度和饱和度

  • hls_palette()
  • l = light亮度
  • s = saturation饱和度
sns.palplot(sns.hls_palette(20,l=0.09,s=0.01))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-89zyAphx-1634142896799)(output_25_0.png)]

# 用于一对儿的数据
sns.palplot(sns.color_palette('Paired',20)) # 一对数据颜色相近

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jwv753UP-1634142896801)(output_26_0.png)]

使用XKCD颜色来命名颜色

# 在指定固定颜色的时候可以用这种方法
plt.plot([0,1],[2,3],sns.xkcd_rgb['red'],lw=4)

plt.plot([0,1],[2,4],sns.xkcd_rgb['green'],lw=3)

plt.plot([0,1],[2,5],sns.xkcd_rgb['blue'],lw=2)
[<matplotlib.lines.Line2D at 0x2598a7b7a88>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OTnOVFJA-1634142896803)(output_28_1.png)]

# 或者使用这种方法指定
colors= ['windows blue','amber','purple']
sns.palplot(sns.xkcd_palette(colors))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xZ6CHuqP-1634142896805)(output_29_0.png)]

连续调色

一般热力图使用

sns.palplot(sns.color_palette('Reds')) # 注意这里要首字母大写。是复数形式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eKcJ2ML2-1634142896808)(output_32_0.png)]

sns.palplot(sns.color_palette('Reds_r')) # 注意这里要首字母大写。是复数形式
# _r = reverse 颠倒

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DNBH5EUm-1634142896813)(output_33_0.png)]

色调线性变换

  • cubehelix_palette()
sns.palplot(sns.color_palette('cubehelix',10)) # 这个颜色还蛮好看的

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qQwLAsll-1634142896816)(output_35_0.png)]

sns.palplot(sns.cubehelix_palette(10,start =-0.1,rot = - 0.75,reverse = True))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8NxPMAkr-1634142896819)(output_36_0.png)]

sns.palplot(sns.cubehelix_palette(10,start =-0.1,rot = - 0.75))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BfhjpfcE-1634142896823)(output_37_0.png)]

sns.palplot(sns.light_palette('red',reverse=True))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7gO8XrC6-1634142896824)(output_38_0.png)]

sns.palplot(sns.dark_palette('red'))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-es69Pehm-1634142896829)(output_39_0.png)]

轮廓图或者叫等高线图

# 参数长度为300个多元正态分布,并做转置
np.random.seed(2)
x,y = np.random.multivariate_normal([0,0],[[1,-0.5],[-0.5,1]],size=300).T 
pal = sns.light_palette('green',as_cmap=True)
sns.kdeplot(x,y,camp=pal)
C:\Users\kingS\anaconda3\lib\site-packages\seaborn\distributions.py:437: UserWarning: The following kwargs were not used by contour: 'camp'
  cset = contour_func(xx, yy, z, n_levels, **kwargs)





<matplotlib.axes._subplots.AxesSubplot at 0x2598ad709c8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ArvysjRQ-1634142896831)(output_41_2.png)]

from scipy import stats,integrate # 导入科学计算库

# 设置主题参数
sns.set(color_codes=True)
np.random.seed(sum(map(ord,'distributions')))
x = np.random.normal(size=100)
sns.distplot(x,kde=False) # kde是是否需要核密度估计的参数
<matplotlib.axes._subplots.AxesSubplot at 0x259906898c8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cLAi2dAL-1634142896834)(output_43_1.png)]

sns.distplot(x,bins = 5,kde=True) # kde是是否需要核密度估计的参数 
# bins 调节多少个区间
<matplotlib.axes._subplots.AxesSubplot at 0x2599066b448>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p5dlj1MK-1634142896835)(output_44_1.png)]

sns.distplot(x,kde=False,fit=stats.gamma) # fit拟合分布函数gamma
<matplotlib.axes._subplots.AxesSubplot at 0x25990ed1408>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x56oh2QK-1634142896838)(output_45_1.png)]

np.random.normal(size=100)
array([-7.93670539e-02,  2.35623791e+00,  1.69823731e-01, -1.65983154e+00,
        1.38854404e+00, -1.56926211e+00, -7.30554288e-01, -6.02577362e-01,
        8.98263749e-01,  7.82067606e-01, -1.41254369e-01, -4.47245943e-01,
        2.91969802e-01,  4.27114940e-01,  5.94314228e-01,  1.53917596e-01,
       -1.53200567e+00, -5.69558781e-01,  7.88319084e-01,  2.82753716e-01,
       -5.86497918e-01, -1.16397748e+00,  2.88947587e-01, -2.63436097e-01,
       -1.50246105e+00, -1.75745611e+00, -1.36483802e+00, -7.33840531e-01,
       -5.64724904e-02,  1.33945114e+00,  2.06119424e+00,  5.25737590e-01,
       -1.97816956e-03, -3.53299540e-01, -3.12083544e-01, -7.51424413e-01,
        5.79962214e-01, -1.76068456e-01, -9.22589067e-01,  4.57838865e-01,
        4.67008945e-01, -8.33531122e-01,  2.33001609e-01, -1.61272298e+00,
        1.48554200e-01, -6.34437667e-01, -2.68405188e-01, -2.83733400e-01,
       -1.46665528e+00, -8.31031975e-01,  2.20334408e-01, -7.42256917e-01,
       -1.03642212e+00,  1.46894035e-01,  1.83128376e+00, -7.97836688e-01,
       -2.51106251e-02, -7.51063377e-01,  2.59559153e-01,  3.74713802e-01,
       -1.62470969e+00,  1.25857468e+00,  1.11413325e-01, -9.66904388e-01,
        5.24940304e-01, -1.04693257e+00,  1.19548873e+00,  8.69765920e-01,
       -5.38073347e-01,  2.78726165e-01,  2.47195055e-01,  3.69496598e-01,
       -2.80957399e-01, -1.70529391e+00,  9.04984230e-01,  1.60629597e+00,
       -2.67589907e-01,  5.65918694e-01, -3.29983562e-01, -1.48961748e-02,
       -1.49366521e+00,  5.76244481e-01,  1.41480402e+00, -3.64581041e-01,
        4.19481870e-01,  6.01141850e-01,  9.62268465e-01, -2.15160561e-01,
        8.14040853e-01, -1.20475982e+00,  5.40559848e-01,  7.75185845e-01,
        1.73212150e+00, -2.17369452e-02, -1.71062699e+00, -1.28783899e+00,
        9.02107169e-01, -1.00361959e+00, -8.49754217e-01, -2.09257982e+00])
np.random.normal(100) # 不加size=1000和加了之后的区别
99.1868940499386
import pandas as pd
# 参数多元分布数据
mean,cov = [0,1],[(1,0.5),(0.5,1)]
data = np.random.multivariate_normal(mean,cov,200)
df = pd.DataFrame(data,columns = ['x','y'])
df
xy
0-0.5794630.414803
10.8808282.285103
2-0.3073320.653860
31.0291921.250403
4-0.8924711.026103
.........
1950.2493141.051238
196-1.2226010.894471
1972.3290062.152045
198-0.1927281.552358
199-0.3256031.476723

200 rows × 2 columns

散点图

sns.jointplot(x = 'x',y = 'y',data=df,color = 'k')
<seaborn.axisgrid.JointGrid at 0x2599217f888>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d6Of2LvY-1634142896842)(output_51_1.png)]

蜂巢散点图

x,y = np.random.multivariate_normal(mean,cov,1000).T
with sns.axes_style('ticks'):
    sns.jointplot(x = x,y = y,kind = 'hex',color = 'k')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WQqAew0v-1634142896843)(output_53_0.png)]

两两关系图-散点图组合

iris = sns.load_dataset('iris')
sns.pairplot(iris)
<seaborn.axisgrid.PairGrid at 0x259940ad288>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NUsnhVES-1634142896844)(output_55_1.png)]

sns回归分析

np.random.seed(sum(map(ord,'regression')))
tips = sns.load_dataset('tips') # 导入tips数据
tips.head(5) # 查看前五行
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4

regplot()和lmplot()都可以画回归关系的图

plt.figure(figsize=(8,6))
sns.regplot(x = 'total_bill',y='tip',data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x25994d9abc8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ecAugFPL-1634142896845)(output_60_1.png)]

sns.set(rc={'figure.figsize':(8,8)})  # 设置画布大小
sns.regplot(data=tips,x='size',y='tip')
<matplotlib.axes._subplots.AxesSubplot at 0x25994b29188>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tA0INbNK-1634142896847)(output_61_1.png)]

sns.regplot(x='size',y = 'tip',data=tips,x_jitter=0.05) # 给x加上随机波动为0.05
<matplotlib.axes._subplots.AxesSubplot at 0x2599539ad48>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-558dOI3F-1634142896848)(output_62_1.png)]

## 类别值的可视化展示
sns.set(style='whitegrid',color_codes=True)

np.random.seed(sum(map(ord,'categorical')))
titanic = sns.load_dataset('titanic')
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
sns.stripplot(x='day',y='total_bill',data=tips,jitter=.1)
<matplotlib.axes._subplots.AxesSubplot at 0x2599553ea08>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GO5PssL7-1634142896849)(output_65_1.png)]

圣诞树图

像圣诞树一样的,和jitter的功能相同

sns.swarmplot(x='day',y='total_bill',data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x2599558ca08>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TG5x0Nbb-1634142896861)(output_67_1.png)]

sns.swarmplot(x='day',y='total_bill',hue='sex',data=tips) # 加一个指标
<matplotlib.axes._subplots.AxesSubplot at 0x259955e5188>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MCNC53s4-1634142896863)(output_68_1.png)]

盒图

  • IQR统计学概念四分位距
  • N = 1.5IQR,若果一个值>Q3+N或者 < Q1-N则为离群点
sns.boxplot(x='day',y='total_bill',hue='time',data=tips)
#,rc={'lines.linewidth':10.5}
<matplotlib.axes._subplots.AxesSubplot at 0x25996ad2e48>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R4st6yT5-1634142896866)(output_71_1.png)]

sns.violinplot(x='total_bill',y='day',hue='time',data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x25996a13f48>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6iGBvgqH-1634142896868)(output_72_1.png)]

sns.violinplot(x='day',y='total_bill',hue='sex',data=tips,split=True)
<matplotlib.axes._subplots.AxesSubplot at 0x25996b65288>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wrtIFgSd-1634142896870)(output_73_1.png)]

sns.violinplot(x='day',y='total_bill',data=tips)
sns.swarmplot(x='day',y='total_bill',data=tips,color='w',alpha=0.5)
<matplotlib.axes._subplots.AxesSubplot at 0x25996e4eec8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mLbZmjfa-1634142896875)(output_74_1.png)]

条形图

表示集中趋势

sns.barplot(x='sex',y='survived',hue='class',data=titanic)
<matplotlib.axes._subplots.AxesSubplot at 0x25996d325c8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YjfaSyzG-1634142896878)(output_77_1.png)]

点图

点图可以更好地描述差异性

sns.pointplot(x='sex',y='survived',hue='class',data=titanic)
<matplotlib.axes._subplots.AxesSubplot at 0x25996db6c48>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BZ2iMEHT-1634142896879)(output_80_1.png)]

点图的优化
  • palette = {}颜色设置
  • markers = []数据点样式
  • linestyles=[]线条样式
sns.pointplot(x='class',y='survived',hue='sex',data=titanic,
             palette={'male':"g",'female':'m'},
              markers=['.','o'],
              linestyles=['-','--'])
<matplotlib.axes._subplots.AxesSubplot at 0x2599725b048>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lNwhiu9Z-1634142896882)(output_82_1.png)]

sns.boxplot(data=iris,orient='v')# orient调整横或竖,'h'=横向,'v'=竖
<matplotlib.axes._subplots.AxesSubplot at 0x2599833a848>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9WllV7HH-1634142896884)(output_83_1.png)]

多层面板分类

sns.factorplot(x='day',y='total_bill',hue='smoker',data=tips,kind='bar')
C:\Users\kingS\anaconda3\lib\site-packages\seaborn\categorical.py:3669: UserWarning: The `factorplot` function has been renamed to `catplot`. The original name will be removed in a future release. Please update your code. Note that the default `kind` in `factorplot` (`'point'`) has changed `'strip'` in `catplot`.
  warnings.warn(msg)





<seaborn.axisgrid.FacetGrid at 0x25998352788>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VpoRYrDX-1634142896885)(output_85_2.png)]

sns.factorplot(x='day',y='total_bill',hue='smoker',col='time',data=tips,kind='swarm')
<seaborn.axisgrid.FacetGrid at 0x259985107c8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H5nFUZ0G-1634142896891)(output_86_1.png)]

sns.factorplot(x='day',y='total_bill',hue='smoker',col='day',data=tips,kind='box',size=4,aspect=0.5)
C:\Users\kingS\anaconda3\lib\site-packages\seaborn\categorical.py:3675: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
  warnings.warn(msg, UserWarning)





<seaborn.axisgrid.FacetGrid at 0x25998548488>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zx6MXNox-1634142896893)(output_87_2.png)]

在这里插入图片描述

tips
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
........................
23929.035.92MaleNoSatDinner3
24027.182.00FemaleYesSatDinner2
24122.672.00MaleYesSatDinner2
24217.821.75MaleNoSatDinner2
24318.783.00FemaleNoThurDinner2

244 rows × 7 columns

g = sns.FacetGrid(tips,col='time')
g.map(plt.hist,'tip')
<seaborn.axisgrid.FacetGrid at 0x25998628b08>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OOwMYkPD-1634142896898)(output_90_1.png)]

g = sns.FacetGrid(tips,col='sex',hue='smoker')
g.map(plt.scatter,'total_bill','tip',alpha=0.5)
g.add_legend() # smoker的标签
<seaborn.axisgrid.FacetGrid at 0x259998e9948>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NCR30Mer-1634142896899)(output_91_1.png)]

g = sns.FacetGrid(tips,row='smoker',col='time',margin_titles=True)
g.map(sns.regplot,'size','total_bill',color='0.1',fit_reg=True,x_jitter=0.1)
<seaborn.axisgrid.FacetGrid at 0x25999ddd188>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gosoTSw0-1634142896901)(output_92_1.png)]

g = sns.FacetGrid(tips,col='day',size=4,aspect=0.5)
g.map(sns.barplot,'sex','total_bill')
C:\Users\kingS\anaconda3\lib\site-packages\seaborn\axisgrid.py:243: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
  warnings.warn(msg, UserWarning)
C:\Users\kingS\anaconda3\lib\site-packages\seaborn\axisgrid.py:728: UserWarning: Using the barplot function without specifying `order` is likely to produce an incorrect plot.
  warnings.warn(warning)





<seaborn.axisgrid.FacetGrid at 0x25999f2dcc8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4kH9U1lc-1634142896904)(output_93_2.png)]

# 导入pandas的类别方法
from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(ordered_days)
ordered_days = Categorical(['Thur','Fri','Sat','Sun'])

g = sns.FacetGrid(tips,row='day',row_order=ordered_days,size = 2,aspect = 4)
g.map(sns.boxplot,'total_bill')
CategoricalIndex(['Sat', 'Sun', 'Thur', 'Fri'], categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False, dtype='category')


C:\Users\kingS\anaconda3\lib\site-packages\seaborn\axisgrid.py:243: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
  warnings.warn(msg, UserWarning)
C:\Users\kingS\anaconda3\lib\site-packages\seaborn\axisgrid.py:728: UserWarning: Using the boxplot function without specifying `order` is likely to produce an incorrect plot.
  warnings.warn(warning)





<seaborn.axisgrid.FacetGrid at 0x259a45378c8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jb6BCxLf-1634142896905)(output_94_3.png)]

pal = dict(Lunch = 'seagreen',Dinner='gray')
g = sns.FacetGrid(tips,hue='time',palette=pal,size=5,hue_kws={'marker':['*','v']})
g.map(plt.scatter,'total_bill','tip',s=50,alpha=0.7,linewidth=0.5,edgecolor='w') # s表示圆圈的大小
g.add_legend()
<seaborn.axisgrid.FacetGrid at 0x259a4d40308>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UJ8HDHNc-1634142896908)(output_95_1.png)]

with sns.axes_style('white'):
    g = sns.FacetGrid(tips,row='sex',col='smoker',margin_titles=True,size=2.5)
g.map(plt.scatter,'total_bill','tip',color='#ff3384',edgecolor='w',lw=0.5)
g.set_axis_labels('Total bill ($)','Tip')
g.set(xticks=[10,30,50],yticks=[2,6,10]) # 调节轴的长度
g.fig.subplots_adjust(wspace=0.02,hspace=0.02) # 调节子图间隔

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FX8jBClG-1634142896909)(output_96_0.png)]

iris = sns.load_dataset('iris')
g = sns.PairGrid(iris)
g.map(plt.scatter)
<seaborn.axisgrid.PairGrid at 0x259a5290e08>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XMmt9bBX-1634142896912)(output_97_1.png)]

g = sns.PairGrid(iris)
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
<seaborn.axisgrid.PairGrid at 0x259a26099c8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k7MxKDBE-1634142896914)(output_98_1.png)]

g = sns.PairGrid(iris,hue = 'species') # 添加区分的类别
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g.add_legend()
<seaborn.axisgrid.PairGrid at 0x259a6e5b688>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tneofLkt-1634142896916)(output_99_1.png)]

g = sns.PairGrid(iris,vars=['sepal_length','sepal_width'],hue='species')
g.map(plt.scatter) # 指定变量画图
<seaborn.axisgrid.PairGrid at 0x259a73e8608>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hZfrufIE-1634142896923)(output_100_1.png)]

g = sns.PairGrid(tips,hue='size',palette='GnBu_d')
g.map(plt.scatter,s=50,edgecolor='w')
g.add_legend()
<seaborn.axisgrid.PairGrid at 0x259a7ff8bc8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9Iz9Dneh-1634142896929)(output_101_1.png)]

热力图

sns.set()
np.random.seed(20)
uniform_data = np.random.normal(size=(3,3))
print(uniform_data)
heatmap = sns.heatmap(uniform_data)
[[ 0.88389311  0.19586502  0.35753652]
 [-2.34326191 -1.08483259  0.55969629]
 [ 0.93946935 -0.97848104  0.50309684]]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e5RY36lx-1634142896932)(output_104_1.png)]

np.random.seed(20)
uniform_data = np.random.normal(size=(3,3))
print(uniform_data)
heatmap = sns.heatmap(uniform_data,vmin=0.2,vmax=0.5,center=0) 
# 调整调色板的区间 vmin and vmax
# 调整调色板的中心 center 
[[ 0.88389311  0.19586502  0.35753652]
 [-2.34326191 -1.08483259  0.55969629]
 [ 0.93946935 -0.97848104  0.50309684]]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZzyIts2p-1634142896934)(output_105_1.png)]

flights = sns.load_dataset('flights')
flights
yearmonthpassengers
01949January112
11949February118
21949March132
31949April129
41949May121
............
1391960August606
1401960September508
1411960October461
1421960November390
1431960December432

144 rows × 3 columns

flights = sns.load_dataset('flights')
flights = flights.pivot('month', 'year', 'passengers')
print(flights)
# 读到表格中
#flight.to_csv("res.csv",sep=',',encoding='gbk')
sns.set(rc={'figure.figsize':(8,8)}) 
ax = sns.heatmap(flights)
year       1949  1950  1951  1952  1953  1954  1955  1956  1957  1958  1959  \
month                                                                         
January     112   115   145   171   196   204   242   284   315   340   360   
February    118   126   150   180   196   188   233   277   301   318   342   
March       132   141   178   193   236   235   267   317   356   362   406   
April       129   135   163   181   235   227   269   313   348   348   396   
May         121   125   172   183   229   234   270   318   355   363   420   
June        135   149   178   218   243   264   315   374   422   435   472   
July        148   170   199   230   264   302   364   413   465   491   548   
August      148   170   199   242   272   293   347   405   467   505   559   
September   136   158   184   209   237   259   312   355   404   404   463   
October     119   133   162   191   211   229   274   306   347   359   407   
November    104   114   146   172   180   203   237   271   305   310   362   
December    118   140   166   194   201   229   278   306   336   337   405   

year       1960  
month            
January     417  
February    391  
March       419  
April       461  
May         472  
June        535  
July        622  
August      606  
September   508  
October     461  
November    390  
December    432  

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zF2njUIa-1634142896935)(output_107_1.png)]


flights = sns.load_dataset('flights')
# 取出这三个属性画热力图,坐标点的位置是passengers
flights = flights.pivot('month', 'year', 'passengers')
ax = sns.heatmap(flights, annot=False, fmt='d',linewidths = 0.05,cmap='PuRd')  #实际的数值注解在图上
# annot 打开数据标签。fmt是调节格式整型,默认是科学计数法的格式
# linewidths 调节间隔
# cmap 调色板

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g1QY3ui7-1634142896938)(output_108_0.png)]

cmap调色板参数请参照!
https://blog.csdn.net/qq_38048756/article/details/118724555

强化学习seaborn 是两个不同的主题,但是可以结使用来绘制强化学习的曲。以下是一个使用 seaborn 绘制强化学习曲线的基本示例: 首先,确保你已经安装了 seaborn 库。如果没有安装,可以使用以下命令进行安装: ``` pip install seaborn ``` 然后,导入 seaborn 和其他必要的库: ```python import seaborn as sns import matplotlib.pyplot as plt ``` 假设你有一个强化学习任务,你已经运行了多个实验,并且每个实验记录了每个回合的奖励值。你可以将这些奖励值绘制成曲线,以观察强化学习算法的学习进展。 下面是一个简单的示例代码,它使用 seaborn 绘制了三个实验的奖励曲线: ```python # 假设你有三个实验的奖励数据 experiment1_rewards = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] experiment2_rewards = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] experiment3_rewards = [0, 3, 6, 9, 12, 15, 18, 21, 24, 27] # 创建一个包含所有实验奖励值的数据框 data = { 'Experiment 1': experiment1_rewards, 'Experiment 2': experiment2_rewards, 'Experiment 3': experiment3_rewards } df = pd.DataFrame(data) # 使用 seaborn 绘制曲线 sns.lineplot(data=df) # 显示图形 plt.show() ``` 运行这段代码,你将会得到一个包含三个实验曲线的图形。 这只是一个简单的示例,你可以根据自己的需求调整绘图参数和数据格式等。希望这个示例能够帮助你开始使用 seaborn 绘制强化学习曲线。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wency(王斯-CUEB)

我不是要饭的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值