脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python可视化学习之seaborn调色盘

Python可视化学习之seaborn调色盘

2022-10-13 12:00qq_21478261 Python

seaborn是在matplotlib基础上封装的,所以matplotlib的调色盘seaborn都可以使用。本文系统介绍seaborn调色盘,相较于matplotlib,有诸多不同,需要的可以参考一下

1、color_palette() 函数

该函数是seaborn选取颜色关键函数

color_palette() will accept the name of any seaborn palette or matplotlib colorma

语法:seaborn.color_palette(palette=None, n_colors=None, desat=None)

import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(dpi=250)
sns.palplot(sns.color_palette())#输出默认颜色

Python可视化学习之seaborn调色盘

print(sns.color_palette())#返回默认颜色元组组成的list

Python可视化学习之seaborn调色盘

#palette,传入colormap名称
sns.palplot(sns.color_palette(palette='Accent'))#使用matplotlib中的colormap

Python可视化学习之seaborn调色盘

#n_colors
sns.palplot(sns.color_palette(n_colors=21))#返回颜色种类,超过了自动循环

Python可视化学习之seaborn调色盘

# desat
sns.palplot(sns.color_palette(n_colors=21,
                           desat=0.2))#设置颜色饱和度

Python可视化学习之seaborn调色盘

#with
plt.figure(dpi=100)
with sns.color_palette(n_colors=21):#循环使用色盘
 _ = plt.plot(np.c_[np.zeros(21), np.arange(21)].T)

Python可视化学习之seaborn调色盘

#传入hex 格式颜色号给sns.color_palette
flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
sns.palplot(sns.color_palette(flatui))

Python可视化学习之seaborn调色盘

#颜色使用
plt.figure(dpi=100)

plt.subplot(1,2,1)
plt.bar([1,2,3],[1,2,3],color=sns.color_palette()[0])#取一种颜色

plt.subplot(1,2,2)
plt.bar([1,2,3],[1,2,3],color=sns.color_palette()[0:3])#取三种颜色

Python可视化学习之seaborn调色盘

 

2、 seaborn可用调色盘

分三大类:‘sequential’(渐变色), ‘diverging’(不可描述,看下图), ‘qualitative’(各种颜色区分鲜明)

choose_colorbrewer_palette函数

该函数可以预览各种颜色盘, 只能在jupyter notebook中使用。

Python可视化学习之seaborn调色盘

Python可视化学习之seaborn调色盘

Python可视化学习之seaborn调色盘

下面详细介绍上面三类颜色。

Qualitative color palettes

to distinguish discrete chunks of data that do not have an inherent ordering,分如下几类:

1、deep, muted, pastel, bright, dark, colorblind

2、hls

3、husl

4、palettable 5、xkcd

6、传入颜色list

#deep, muted, pastel, bright, dark, colorblind
for i in list('deep, muted, pastel, bright, dark, colorblind'.split(', ')): 
  print(i,end='\t')
  sns.palplot(sns.color_palette(palette=i))  

从上到下依次为:deep, muted, pastel, bright, dark, colorblind

Python可视化学习之seaborn调色盘

# hls

sns.palplot(sns.color_palette(palette='hls'))
sns.palplot(sns.hls_palette(8, l=.3, s=.8))

Python可视化学习之seaborn调色盘

#husl

sns.palplot(sns.color_palette(palette='husl'))
sns.palplot(sns.color_palette("husl", 8))

Python可视化学习之seaborn调色盘

import palettable#python palettable库
sns.palplot(sns.color_palette(palette=palettable.colorbrewer.qualitative.Dark2_7.mpl_colors))#使用palettable中的colormap
sns.palplot(sns.color_palette(palette=palettable.scientific.sequential.Nuuk_7.mpl_colors))

Python可视化学习之seaborn调色盘

#xkcd
plt.plot([0, 1], [0, 1], sns.xkcd_rgb["pale red"], lw=3)
plt.plot([0, 1], [0, 2], sns.xkcd_rgb["medium green"], lw=3)
plt.plot([0, 1], [0, 3], sns.xkcd_rgb["denim blue"], lw=3)

Python可视化学习之seaborn调色盘

xkcd,详细可参考 :Python可视化学习之matplotlib内置单颜色

#传入颜色list给ns.xkcd_palette()
colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
sns.palplot(sns.xkcd_palette(colors))

Python可视化学习之seaborn调色盘

Sequential color palettes

is appropriate when data range from relatively low or uninteresting values to relatively high or interesting values

1、"Blues"这类

2、'cubehelix',seaborn.cubehelix_palette(n_colors=6, start=0, rot=0.4, gamma=1.0, hue=0.8, light=0.85, dark=0.15, reverse=False, as_cmap=False)

3、传统色的渐变色,light_palette()、dark_palette()

#"Blues"这类渐变色
sns.palplot(sns.color_palette("Blues"))
sns.palplot(sns.color_palette("Blues_d"))#_d表示显示该颜色的深色系(“dark” palettes by appending “_d”)
sns.palplot(sns.color_palette("Blues_r"))

Python可视化学习之seaborn调色盘

# cubehelix
sns.palplot(sns.color_palette("cubehelix", 8))
sns.palplot(sns.color_palette("ch:2.5,-.2,dark=.3"))#使用cubehelix接口制作颜色
sns.palplot(sns.cubehelix_palette(8, start=2, rot=0, dark=0, light=.95, reverse=True))

Python可视化学习之seaborn调色盘

#light_palette
sns.palplot(sns.light_palette("seagreen", reverse=True))
sns.palplot(sns.light_palette((260, 75, 60), input="husl"))

Python可视化学习之seaborn调色盘

Diverging color palettes

for data where both large low and high values are interesting.

1、diverging_palette()

sns.palplot(sns.color_palette("coolwarm", 7))

Python可视化学习之seaborn调色盘

sns.palplot(sns.diverging_palette(240, 10, n=9))
sns.palplot(sns.diverging_palette(150, 275, s=80, l=55, n=9))
sns.palplot(sns.diverging_palette(250, 15, s=75, l=40,
                                n=9, center="dark"))

Python可视化学习之seaborn调色盘

到此这篇关于Python可视化学习之seaborn调色盘的文章就介绍到这了,更多相关Python seaborn调色盘内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_21478261/article/details/107945630

延伸 · 阅读

精彩推荐