Python如何查看并打印matplotlib中所有的colormap(cmap)类型
作者:勤奋的大熊猫 发布时间:2023-12-05 11:04:28
标签:Python,查看,打印,matplotlib,colormap
查看并打印matplotlib中所有的colormap(cmap)类型
代码如下:
方法一
import matplotlib.pyplot as plt
cmaps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))
print(cmaps)
我们忽略以_r结尾的类型,因为它们都是类型后面不带有_r的反转版本(reversed version)。
所有的类型我们可以在matplotlib的源代码中找到:(如下图)
方法二
import matplotlib.pyplot as plt
cmap_list1 = plt.colormaps()
print(cmap_list1)
方法三
如果使用的是Pycharm编译器,那么可以在作图的时候简单的随便给定一个cmap的类型,如果给定的cmap类型是错误的,那么在编译器的错误提示信息中也会显示出所有的cmap类型。
比如,我们这里我们想要做一个高斯函数的曲面分布图,我们随意给cmap一个'aaa'的值,这时,我们可以在编译器提示窗口看到如下错误信息的输出。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
x, y = np.meshgrid(x, y)
w0 = 1
gaussian = np.exp(-((pow(x, 2) + pow(y, 2)) / pow(w0, 2)))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x, y, gaussian, cmap='aaa')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
"""
错误提示信息:
ValueError: 'aaa' is not a valid value for name; supported values are 'Accent',
'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu',
'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens',
'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r',
'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2',
'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr',
'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy',
'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds',
'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral',
'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r',
'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn',
'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr',
'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r',
'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r','gist_earth',
'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat','gist_heat_r', 'gist_ncar',
'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r','gist_stern', 'gist_stern_r',
'gist_yarg', 'gist_yarg_r', 'gnuplot','gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray',
'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet','jet_r',
'magma', 'magma_r','nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r',
'pink', 'pink_r','plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r',
'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10','tab10_r',
'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain','terrain_r',
'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted','twilight_shifted_r',
'viridis', 'viridis_r', 'winter', 'winter_r'
"""
matplotlib cmap取值问题
直接定义一个类来获取cmap中各个颜色方便使用
使用的话:mycolor = MyColor(‘Accent’); mycolor.get_color();# 每次就调用获取下一个cmap中的颜色。
class MyColor(object):
def __init__(self, cmap_name):
self.color_set = plt.get_cmap(cmap_name).colors
self.idx = 0
self.color_len = len(self.color_set)
def get_color(self):
if self.idx == self.color_len - 1:
self.idx = 0
color = self.color_set[self.idx]
self.idx += 1
return color
可视化官方提供的cmap
比如查看:[‘Pastel1’, ‘Pastel2’, ‘Paired’, ‘Accent’, ‘Dark2’, ‘Set1’, ‘Set2’, ‘Set3’, ‘tab10’, ‘tab20’, ‘tab20b’, ‘tab20c’]
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
cmaps = {}
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
def plot_color_gradients(category, cmap_list):
# Create figure and adjust figure height to number of colormaps
nrows = len(cmap_list)
figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh), dpi=100)
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
left=0.2, right=0.99)
axs[0].set_title(f'{category} colormaps', fontsize=14)
for ax, name in zip(axs, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
transform=ax.transAxes)
# Turn off *all* ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()
# Save colormap list for later.
cmaps[category] = cmap_list
plot_color_gradients('Qualitative',
['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
'tab20c'])
运行后:
来源:https://blog.csdn.net/u011699626/article/details/108458947


猜你喜欢
- 常用的消息摘要算法有MD5和SHA,这些算法在python和go的库中都有,需要时候调用下就OK了,这里总结下python和go的实现。一、
- 一条撤回的微信消息,就像一个秘密,让你迫切地想去一探究竟;或如一个诱饵,瞬间勾起你强烈的兴趣。你想知道,那是怎样的一句话?是对方不慎讲出的真
- 对于初学者,入门至关重要,这关系到初学者是从入门到精通还是从入门到放弃。以下是结合Python的学习经验,整理出的一条学习路径,主要有四个阶
- 刚才要说的是这几天亲身体验了一下ebay的AIR感觉挺不错的,无论从界面,交互,功能上都感觉挺好的。关于topic中的“剑走偏锋”是因为我认
- 函数:split()例子我们想要将以下字符串rule进行拆分。字符串表示的是一个规则,由“…”得到“…”。我们需要将规则中的条件属性与取值分
- 第7个PPT的代码是用 JS 去设置 CSS,这与“不同浏览器解析DOM不同”没有任何关系,是CSS的兼容性!而且用JS去直接设样式是技术理
- 不得不说python的上手非常简单。在网上找了一下,大都是python2的帖子,于是随手写了个python3的。代码非常简单就不解释了,直接
- 在GitHub上发现一些很有意思的项目,由于本人作为Python的初学者,编程代码能力相对薄弱,为了加强Python的学习,特此利用前辈们的
- 由于刚刚学习python,对PyCharm也不是很熟悉,在成功运行多个死循环程序而没有关闭它的情况下,PyCharm成功的经常无响应,反应缓
- 本文实例为大家分享了基于TensorFlow的CNN实现Mnist手写数字识别的具体代码,供大家参考,具体内容如下一、CNN模型结构输入层:
- 前言飞桨(PaddlePaddle)是集深度学习核心框架、工具组件和服务平台为一体的技术先进、功能完备的开源深度学习平台1. 任务描述乘坐出
- ASP中查询数据库记录写入XML文件示例,把下面代码保存为Asp_XML.asp运行即可: &
- 防止Application对象在多线程访问中出现错误asp代码处理代码如下(VB):<%Application.Lock()Appli
- 前言加密技术在数据安全存储,数据传输中发挥着重要作用,能够保护用户隐私数据安全,防止信息窃取。RSA是一种非对称加密技术,在软件、网页中已得
- 因工作需要研究了支付宝即时到帐接口,并成功应用到网站上,把过程拿出来分享。即时到帐只是支付宝众多商家服务中的一个,表示客户付款,客户用支付宝
- 问题描述分析这是因为本地delve组件版本过低导致的,2019.2.1版本的Goland默认支持go 1.13查看F:\Go (GOPATH
- 代码问题:控制台和日志的文件的等级设置要放在logger = logging.getLogger('myloger')实例化
- 如果说亲密性原则是对元素的归类组合,是将元素之间逻辑理解上的差异在视觉上表现出来,是属于信息分类的话,那么对齐原则即是在视觉上串起这些差异化
- python实现银行管理系统,供大家参考,具体内容如下有的地方用的方法的比较复杂,主要是为回顾更多的知识test1用来存类和函数#test1
- 本文实例分析了PHP中怎样防止SQL注入。分享给大家供大家参考。具体分析如下:一、问题描述:如果用户输入的数据在未经处理的情况下插入到一条S