数据可视化_seaborn分布数据可视化

本文详细介绍了如何使用Seaborn进行数据可视化,包括整体风格设置如sns.set()、sns.set_style()、sns.despine()、axes_style()、set_context()和color_palette(),以及分布数据的可视化方法,如直方图、密度图、散点图和矩阵散点图。通过示例代码展示了如何创建和定制这些图表,帮助读者深入理解Seaborn的使用技巧。
摘要由CSDN通过智能技术生成

seaborn整体风格设置

sns.set() → 整体设置seaborn的主题,调色板,颜色代码等多个样式

# 设置cell多行输出

from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = 'all' #默认为'last'

# 导入相关库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns   # 导入seaborn库
import os
import warnings
%matplotlib inline

warnings.filterwarnings('ignore')
# 数据
years = [1950,1960,1970,1980,1990,2000,2010]
gdp = np.random.rand(7)*1000

data = pd.DataFrame(gdp,index=years)
# 整体设置seaborn的主题风格,调色板,颜色
sns.set(style='whitegrid',palette='muted',color_codes=True)
# style 主题风格 包括:"white", "dark", "whitegrid", "darkgrid", "ticks"
# palette 调色板
# color_codes 颜色代码
plt.plot(np.arange(10))
plt.xlim([0,10])
plt.ylim([0,10])

在这里插入图片描述

sns.set_style() → 切换seaborn图表风格

单独改变seaborn的主题样式,seaborn有5种预先设计号的主题样式:

  • darkgrid(默认使用该主题样式)
  • dark
  • whitegrid
  • white
  • ticks
sns.set_style('dark')   # 单独设置主题风格
plt.scatter(years,gdp,)  # 仍然可以使用matplotlib的参数

在这里插入图片描述

sns.despine() → 设置坐标轴

→ white,ticks主题有4个坐标轴,可以用sns.despine()将右侧和顶部的坐标轴去除

seaborn.despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False, offset=None, trim=False)

  • offset,设置与坐标轴之间的偏移
  • trim,为True时,将坐标轴限制在数据最大最小值
sns.set_style('ticks')
plt.plot(np.random.rand(10))
sns.despine()  # 默认去掉顶部和右侧坐标轴

在这里插入图片描述

plt.plot(np.random.rand(20),color='r')
sns.despine(offset=10,trim=True)
# offset:与坐标轴之间的偏移
# trim,为True时,将坐标轴限制在数据最大最小值

在这里插入图片描述

axes_style() → 设置局部图表风格

单独设置某个子图的风格

# 可和with配合的用法
with sns.axes_style("darkgrid"):
    plt.subplot(211)
    plt.plot(np.random.rand(20))
# 设置局部图表风格,用with做代码块区分

sns.set_style("whitegrid")
plt.subplot(212)
plt.plot(np.random.rand(20))
sns.despine()
# 外部表格风格

在这里插入图片描述

set_context() → 设置显示比例尺度

→ set_context()的选择包括:‘paper’, ‘notebook’, ‘talk’, ‘poster’,默认为notebook

sns.set_context('talk')
plt.plot(np.random.rand(20))
plt.xlim([0,19])
plt.ylim([0,1])

在这里插入图片描述

color_palette() → 设置调色盘

→对图表整体颜色、比例等进行风格设置,包括颜色色板等,调用系统风格进行数据可视化

color_palette()默认6种颜色:deep, muted, pastel, bright, dark, colorblind

seaborn.color_palette(palette=None, n_colors=None, desat=None)

其他颜色风格
风格内容:Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu,
BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r,
Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples,
Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3,
Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r……

# 设置调色板后,绘图创建图表
sns.color_palette('icefire_r')  # 设置调色板
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值