本篇详细介绍matplotlib内置的颜色条Colormap使用。
首发于本人公众号:pythonic生物人
本文将学到什么?
1、colormap名称
2、colormap可视化
3、colormap使用方法
4、参考资料
更好的阅读体验请戳:
1、colormap名称
colormap颜色通过matplotlib的cm模块调用,print(dir(cm))即可输出所有的名称,共计81种Colormap(不包含反向色条),例如
'Reds'的反向色条'Reds_r'),详细如下:
['Accent', 'Blues', 'BrBG', 'BuGn', 'BuPu', 'CMRmap', 'Dark2', 'GnBu', 'Greens', 'Greys', 'OrRd', 'Oranges', 'PRGn', 'Paired', 'Pastel1', 'Pastel2', 'PiYG', 'PuBu', 'PuBuGn', 'PuOr', 'PuRd', 'Purples', 'RdBu', 'RdGy', 'RdPu', 'RdYlBu', 'RdYlGn', 'Reds', 'Set1', 'Set2', 'Set3', 'Spectral', 'Wistia', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd', 'afmhot', 'autumn', 'binary', 'bone', 'brg', 'bwr', 'cividis', 'cool', 'coolwarm', 'copper', 'cubehelix', 'flag', 'gist_earth', 'gist_gray', 'gist_heat', 'gist_ncar', 'gist_stern', 'gist_yarg', 'gnuplot', 'gnuplot2', 'gray', 'hot', 'hsv', 'inferno', 'jet', 'magma', 'nipy_spectral', 'ocean', 'pink', 'plasma', 'prism', 'rainbow', 'seismic', 'spring', 'summer', 'tab10', 'tab20', 'tab20b', 'tab20c', 'terrain', 'twilight', 'twilight_shifted', 'viridis', 'winter']
2、 colormap可视化
每个 colormap到底包含哪些颜色了,上图:3、colormap使用方法
colormap分ListedColormap(下图中Accent)和LinearSegmentedColormap(下图中Blues)两种,前一种存储特定的颜色(比如说红白黑三种),使用colors可以取出所有的RGBA色号值;后一种可能是渐变色无colors属性。下面详细介绍怎么使用 colormap。
import matplotlib.pyplot as plt
from matplotlib import cm
plt.figure(dpi=150)
##ListedColormap
#取多种颜色
plt.subplot(1,4,1)
#plt.bar(range(5),range(1,6),color=plt.cm.Accent(range(5)))
#plt.bar(range(5),range(1,6),color=plt.cm.get_cmap('Accent')(range(5)))
plt.bar(range(5),range(1,6),color=plt.get_cmap('Accent')(range(5)))
#取某一种颜色
plt.subplot(1,4,2)
plt.bar(range(5),range(1,6),color=plt.cm.Accent(4))
##LinearSegmentedColormap
#取多种颜色
plt.subplot(1,4,3)
plt.bar(range(5),range(1,6),color=plt.get_cmap('Blues')(np.linspace(0, 1, 5)))
#取一种颜色
plt.subplot(1,4,4)
plt.bar(range(5),range(1,6),color=plt.get_cmap('Blues')(3))
4、参考资料
欢迎关注公众号:pythonic生物人

网友评论