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') # 设置调色板