pythonic生物人

配色工具,数千类colormap供选择!

本次分享一个Python颜色条(colormap)工具palettable的使用。 palettable中colormap多达3174类,单颜色数万种,适合地理、海洋科学等多个领域。

palettable安装

pip install palettable #安装palettable

colormap手动查阅(不推荐)

palettable中colormap多达3174类,官网https://jiffyclub.github.io/palettable点击查阅,

colormap本地快速查阅(🔥推荐)

翻阅很不方便,小编已将官网 爬下来制作成cheatsheet : 已免费加入教程👉: Matplotlib繁琐,试试Seaborn 快速翻阅PDF cheatsheet找到感兴趣的colormap name, 以colorbrewer中的PuBu_3为例, 在 本地文件 (已免费加入教程👉 Matplotlib繁琐,试试Seaborn )中查找, 找到palettable.colorbrewer.sequential.PuBu_3后即可使用。

seaborn中使用palettable

palettable colormap使用很简单,看代码中注释文字,
import matplotlib.pyplot as plt
import palettable  #加载palettable包

f, axs = plt.subplots(ncols=2,
                      figsize=(8, 4),
                      gridspec_kw=dict(width_ratios=[4, 3]))

sns.scatterplot(
    data=penguins,
    x="喙深 (毫米)",
    y="喙长 (毫米)",
    #使用palettable中colorbrewer下的PuBu_3
    palette=palettable.colorbrewer.sequential.PuBu_3_r.mpl_colors[0:3],
    s=300,
    hue="企鹅的种类",
    ax=axs[0])
sns.histplot(
    data=penguins,
    x="企鹅的种类",
    hue="企鹅的种类",
    #使用palettable中colorbrewer下的PuBu_3
    palette=palettable.colorbrewer.sequential.PuBu_3_r.mpl_colors[0:3],
    shrink=.8,
    legend=False,
    ax=axs[1])
f.tight_layout()
更多例子,

R、Matlab、Origin等使用palettable

palettable中包含 colors、hex_colors和mpl_colors三个函数 ,分别取出RGB格式、hex格式和RGB元组格式颜色号, 取出来的颜色号是一个列表,选取感兴趣的颜色使用在Python、R、Matlab、Excel、Origin等中都可以, 举一个R中的例子,
options(repr.plot.width = 6, repr.plot.height = 6, repr.plot.res = 200)
# 复制palettable.tableau.BlueRed_6中的颜色
pal_r <- c('#2C69B0', '#F02720', '#AC613C', '#6BA3D6', '#EA6B73')  

par(mfrow = c(1, 2))
pie(rep(1, 5), col = pal_r, radius = 1.5)
barplot(1:5, col = pal_r)
mtext("palettable in R", outer = TRUE, cex = 1.5, line = -2)
-END-
更多Python可视化模版👇 交流学习👇
图片