图表可视化seaborn风格和调色盘
seaborn是基于matplotlib的python數(shù)據(jù)可視化庫(kù),提供更高層次的API封裝,包括一些高級(jí)圖表可視化等工具。
使用seaborn需要先安裝改模塊pip3 install seaborn 。
?
一、風(fēng)格style
包括set() / set_style() / axes_style() / despine() / set_context()
創(chuàng)建正弦函數(shù)并顯示圖表
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline def sinplot(flip = 1):x = np.linspace(0,14,100)for i in range(1,7):plt.plot(x,np.sin(x+i)*i) # 6個(gè)正弦函數(shù) sinplot()1.set(),設(shè)置整體為默認(rèn)風(fēng)格
sns.set() #默認(rèn)風(fēng)格為darkgrid sinplot()2.set_style(),自定義整體風(fēng)格
參數(shù)為"white"、"dark"、 "whitegrid"、 "darkgrid"、 "ticks"或者None,默認(rèn)為darkgrid
fig = plt.figure(figsize=(15,6))ax1 = fig.add_subplot(121) sns.set_style('whitegrid') data = np.random.normal(size=(20, 6)) + np.arange(6) / 2 sns.boxplot(data=data) plt.title('style - whitegrid')# 仍然可以使用matplotlib的參數(shù) ax2 = fig.add_subplot(122) # sns.set_style("dark") sinplot()3.axes_style(),設(shè)置子圖風(fēng)格
可與with搭配使用,設(shè)置with代碼塊內(nèi)的圖表風(fēng)格,不影響整體圖表風(fēng)格。
fig = plt.figure(figsize=(15,6)) with sns.axes_style("dark"): #只對(duì)with代碼塊內(nèi)的圖表風(fēng)格生效,即只對(duì)第一個(gè)子圖生效plt.subplot(121) sinplot()sns.set_style("white") #整體風(fēng)格為white plt.subplot(122) sinplot()?
4.despine()移除軸線
despine(fig=None, ax=None, top=True, right=True, left=False,?bottom=False, offset=None, trim=False)
top、right、left、bottom:上、右、左、下方軸線,默認(rèn)移除上方和右側(cè)軸線
offset:xy軸和y軸的起點(diǎn)相對(duì)原始位置的偏移量
trim:默認(rèn)坐標(biāo)軸長(zhǎng)度沒有限制,會(huì)延伸到圖表內(nèi)容結(jié)束,True表示將坐標(biāo)軸的顯示的長(zhǎng)度在最小值和最大值之間
fig = plt.figure(figsize=(20,6)) ax1 = fig.add_subplot(131) sinplot() sns.despine()# 默認(rèn)刪除上、右坐標(biāo)軸 ax2 = fig.add_subplot(132) sns.violinplot(data=data) #小提琴圖 # sns.despine(offset=1, trim=True) ax3 = fig.add_subplot(133) sns.boxplot(data=data, palette="deep") sns.despine(left=True, right = False) #最終是該despine設(shè)置生效5.set_context()顯示比例
可選參數(shù)為'paper'、 'notebook'、'talk'、'poster',默認(rèn)為notebook,設(shè)置標(biāo)簽、線等的大小。
sns.set_context("notebook") sinplot()下面分別為設(shè)置為notebook、paper、talk和poster的顯示結(jié)果。
?
二、 調(diào)色盤
1.color_palette()
默認(rèn)取當(dāng)前調(diào)色盤的顏色,返回結(jié)果是一個(gè)seaborn.palette的類,形式類似一個(gè)列表,列表中每一個(gè)元素為元組,元組用3個(gè)數(shù)值表示rgb顏色。
current_palette = sns.color_palette() # 讀取當(dāng)前調(diào)色盤顏色,可添加參數(shù)n表示取幾個(gè)色塊 print(current_pallette,type(current_palette)) sns.palplot(current_palette) #<class 'seaborn.palettes._ColorPalette'> [(0.9913725490196079, 0.7913725490196079, 0.7082352941176471)...]seaborn可用調(diào)色盤有6種,deep、 muted、bright、 pastel、dark、colorblind,默認(rèn)顯示bright。
?
其他調(diào)色盤
sns.color_palette('Reds', 10),第一個(gè)參數(shù)表示色系,第二個(gè)參數(shù)表示取幾個(gè)色塊。
顏色默認(rèn)是由淺到深,帶r表示反轉(zhuǎn)即顏色由深到淺,不是所有顏色都可以翻轉(zhuǎn)哦。
#其他可用調(diào)色盤 #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, autumn, autumn_r #binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, #cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, #gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, #gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, #nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, #seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, #terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r sns.palplot(sns.color_palette('Reds', 10)) sns.palplot(sns.color_palette('Greens_r', 7)) #?
# 分組顏色,同一個(gè)顏色成對(duì)出現(xiàn) sns.palplot(sns.color_palette('Paired',7)) #參數(shù)可以為奇數(shù) sns.palplot(sns.color_palette('Paired', 18))?
2.設(shè)置調(diào)色盤
set_palette(palette, n_colors=None, desat=None, color_codes=False),使用Seaborn調(diào)色盤設(shè)置Matplotlib顏色循環(huán)
palette參數(shù)可設(shè)置為seaborn color paltte | matplotlib colormap | hls | husl
sns.set_palette('Greens') sinplot()3.亮度和飽和度
sns.hls_palette(n_colors=6, h=.01, l=.6, s=.65)
sns.husl_palette(n_colors=6, h=.01, s=.9, l=.65),兩者表示亮度和飽和度的參數(shù)位置相反。
參數(shù)n_colors表示取幾個(gè)色塊,h表示第一個(gè)色塊的顏色,l表示亮度,s表示飽和度,h、l、s取值[0,1]。
sns.palplot(sns.hls_palette(8,0.01,0.5,0.5)) sns.palplot(sns.husl_palette(8,0.03,0.8,0.8))?
4.按線性增長(zhǎng)設(shè)置顏色
cubehelix_palette(n_colors=6, start=0, rot=.4, gamma=1.0, hue=0.8,light=.85, dark=.15, reverse=False, as_cmap=False)
c_colors:色塊個(gè)數(shù)
start:色塊的起點(diǎn)顏色,[0,3]之間
rot:顏色的旋轉(zhuǎn)角度
gamma:顏色的伽馬值,值越大顏色越深
hue:飽和度,[0,1]之間
light和dark:亮度和深度,[0,1]之間
reverse:默認(rèn)為False顏色由淺到深,True表示由深到淺
as_cmp:If True, return a matplotlib colormap instead of a list of colors
sns.palplot(sns.cubehelix_palette(8, gamma=1.5)) sns.palplot(sns.cubehelix_palette(8, start=1, rot=-0.75)) sns.palplot(sns.cubehelix_palette(8, start=2, rot=0, dark=0.5,light=0.9, reverse=True))5.按顏色深淺設(shè)置顏色
light_palette(color, n_colors=6, reverse=False, as_cmap=False,input="rgb")和dark_palette(color, n_colors=6, reverse=False, as_cmap=False, input="rgb")
color_palette()中的顏色參數(shù)為調(diào)色盤,而light_palette()和dark_palette()中的color顏色參數(shù)就是單純的顏色,例如對(duì)于藍(lán)色,color_palette()需設(shè)置Blues,后兩者參數(shù)為blue。
sns.palplot(sns.light_palette("red")) sns.palplot(sns.dark_palette("red")) sns.palplot(sns.light_palette("blue")) sns.palplot(sns.dark_palette("blue", reverse=True))6.設(shè)置分散顏色
diverging_palette(h_neg, h_pos, s=75, l=50, sep=10, n=6,?center="light", as_cmap=False)
h_neg, h_pos:起始和終止顏色,[0,359]之間
s、l:飽和度和亮度,[0,100]之間
n:色塊個(gè)數(shù)
center: 最中間顏色為淺色或者深色,{'light','dark'},默認(rèn)為淺色
sns.palplot(sns.diverging_palette(0,150, s=60, l=20, n=8)) sns.palplot(sns.diverging_palette(300, 150, s=30, l=50, n=8,center='dark'))?
sns.set_style("white")# 設(shè)置風(fēng)格 fig = plt.figure(figsize=(18,5))with sns.color_palette("Greens"): #設(shè)置局部調(diào)色盤plt.subplot(131)sinplot()sns.set_palette("husl") #對(duì)于多系列的圖表,用不同顏色區(qū)分系列 plt.subplot(132) sinplot()x = np.arange(25).reshape(5, 5) cmap = sns.diverging_palette(200, 20, sep=20, as_cmap=True) plt.subplot(133) sns.heatmap(x, cmap=cmap) #顯示熱力圖效果?
轉(zhuǎn)載于:https://www.cnblogs.com/Forever77/p/11396588.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的图表可视化seaborn风格和调色盘的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到拉沙子是什么意思
- 下一篇: seaborn分布数据可视化:直方图|密