Python中如何使用NCL的全部色表Colormaps?

本文同步发布于公众号『气象学家』传送门。NCL中丰富的Color Table,而在Python中matplotlib绘图工具包中默认的类型可能略显不足。所以,今天介绍一下Dr.🐱开源的cmaps库,即可将NCL的丰富多彩,完美地引入到Python绘图中来。另外,给Dr.🐱“打广告”,欢迎在GitHub(<<项目地址>>https://github.com/hhuangwx/cmaps)上Star或Fork.

cmaps简介

Make it easier to use user defined colormaps in matplotlib. Default colormaps are from NCL website.

Users can define a environmental variable CMAP_DIR pointing to the folder containing the self-defined rgb files.

安装 Installation

pip install https://github.com/hhuangwx/cmaps/archive/master.zip

or:

git clone https://github.com/hhuangwx/cmaps.git
cd cmaps
python setup.py install

使用 Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import matplotlib.pyplot as plt
import cmaps
import numpy as np

x = y = np.arange(-3.0, 3.01, 0.05)
X, Y = np.meshgrid(x, y)

sigmax = sigmay = 1.0
mux = muy = sigmaxy=0.0

Xmu = X-mux
Ymu = Y-muy

rho = sigmaxy/(sigmax*sigmay)
z = Xmu**2/sigmax**2 + Ymu**2/sigmay**2 - 2*rho*Xmu*Ymu/(sigmax*sigmay)
denom = 2*np.pi*sigmax*sigmay*np.sqrt(1-rho**2)
Z = np.exp(-z/(2*(1-rho**2))) / denom

plt.pcolormesh(X,Y,Z,cmap=cmaps.WhiteBlueGreenYellowRed)
plt.colorbar()

罗列色图

List the colormaps using the code in the examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import cmaps
import numpy as np
import inspect

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('text', usetex=False)


def list_cmaps():
attributes = inspect.getmembers(cmaps, lambda _: not (inspect.isroutine(_)))
colors = [_[0] for _ in attributes if
not (_[0].startswith('__') and _[0].endswith('__'))]
return colors


if __name__ == '__main__':
color = list_cmaps()

a = np.outer(np.arange(0, 1, 0.001), np.ones(10))
plt.figure(figsize=(20, 20))
plt.subplots_adjust(top=0.95, bottom=0.05, left=0.01, right=0.99)
ncmaps = len(color)
nrows = 8
for i, k in enumerate(color):
plt.subplot(nrows, ncmaps // nrows + 1, i + 1)
plt.axis('off')
plt.imshow(a, aspect='auto', cmap=getattr(cmaps, k), origin='lower')
plt.title(k, rotation=90, fontsize=10)
plt.title(k, fontsize=10)
plt.savefig('colormaps.png', dpi=300)

参考:

链接.1
链接.2
链接.3

有任何问题都欢迎交流探讨,共同学习进步!


-------------本文结束 感谢阅读-------------
作者Gavin
有问题请在相应页面留言(评论系统DISQUS需要"翻墙"才可见)
或者私信给我 GMAIL: zhpfu.atm@gmail.com
满分是10分的话,这篇文章你给几分
--> -->