pythonic生物人

相见恨晚的Python可视化工具

本次分享一个Python可视化颜色条(colormap)工具:palettable。

palettable中colormap多达3174类,单颜色数万种,适合地理、海洋科学等多个领域。


palettable安装

pip install palettable #安装palettable

colormap手动查阅(不推荐)

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


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

翻阅很不方便,小编已将官网爬下来制作成cheatsheet :

已免费加入教程👉:Matplotlib繁琐,试试Seaborn

快速翻阅PDF cheatsheet找到感兴趣的colormap name,Image

以colorbrewer中的PuBu_3为例,

在本地文件(已免费加入教程👉Matplotlib繁琐,试试Seaborn)中查找,Image

找到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()

Image

更多例子,

Image


R、Matlab、Origin等使用palettable

palettable中包含colors、hex_colors和mpl_colors三个函数,分别取出RGB格式、hex格式和RGB元组格式颜色号,Image

取出来的颜色号是一个列表,选取感兴趣的颜色使用在Python、R、Matlab、Excel、Origin等中都可以,

举一个R中的例子,Image

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)

Image

-END-
分享一份原创Python可视化教程(基于py可视化利器seaborn):530张图形+8000行代码+详细代码注释+后续免费更新+学习交流群,教程部分内容,

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

每类图表包含详细代码+详细代码注释,多达8000行代码,例如,Image


如何加入学习?

👇加入学习(收费、备注:169)

Image

✅解锁获取6大权限:

  • 8000+行代码(复制即可运行)
  • 在线教程文档(清晰目录)
  • 依赖数据(免费下载)
  • 交流群
  • 后续更新
  • 定期抽最新机器学习、数据科学类书籍