温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中seaborn调色盘的示例分析

发布时间:2022-02-25 09:21:28 来源:亿速云 阅读:318 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“Python中seaborn调色盘的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Python中seaborn调色盘的示例分析”这篇文章吧。

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调色盘的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI