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
0
投稿
猜你喜欢
- 本文实例为大家分享了基于信息增益的决策树归纳的Python实现代码,供大家参考,具体内容如下# -*- coding: utf-8 -*-i
- 本文实例讲述了python版本的读写锁操作方法。分享给大家供大家参考,具体如下:最近要用到读写锁的机制,但是python2.7的自带库里居然
- 在使用mysql视图是出现问题: The user specified as a definer ('root'@'
- 什么是 manimManim 是一个用于精确编程动画的引擎,专为创建解释性数学视频而设计。注意,有两个主要版本的 manim。该存储库最初是
- 我们都知道在9i之前,要想获得建表和索引的语句是一件很麻烦的事。我们通常的做法都是通过export with rows=no来得到,但它的输
- 作为微软推出的网页与数据库解决方案,ASP由于有微软得天独厚的操作系统等技术后盾支持,因此得到了迅速的发展,并且正受到越来越多的欢迎,在目前
- 今天有个需要需要传递中文参数给URL但是在GBK环境下的脚本传递GBK的参数老是给我报UNICODE的解码错误。烦的很。所以我们果断选择用u
- 今天终于可以用wxPython开发GUI程序了,非常高兴。把其中的一些注意点写下来以供参考。在windows XP平台下,首先需要做以下环境
- 一、日期类型:对于SQL Server 2008 来说(因为2000甚至2005已经稍微有被淘汰的迹象,所以在此不作过多说明,加上自己工作使
- 首先要声明一点,大部分情况下,修改MySQL是需要有mysql里的root权限的, 所以一般用户无法更改密码,除非请求管理员。方法一使用ph
- <%@ language=vbscript codepage=65001%> <
- docutils 的官方工具地址为:https://docutils.sourceforge.io/目前的更新主要是在版本和使用手册的更新上
- 直方图是用于展示数据的分组分布状态的一种图形,用矩形的宽度和高度表示频数分布,通过直方图,用户可以很直观的看出数据分布的形状、中心位置以及数
- 下表列出了 Microsoft ACCESS 的命令行选项:选项 效果database 打开指定的 Microsoft Access 数据库
- 前言:话说,我一直没能理解美工究竟是什么(这是一篇投稿)。因为要求确实很多。目前,我只能理解成,是前端开发+页面设计+用户体验设计的全能手。
- 方法一import randomimport numpy as npfrom PIL import Image, ImageOps, Ima
- python excel文件(.xls文件)如何处理xlrd 用于读取文件,xlwt 用于写入文件,xlutils 是两个工具包的桥梁,也就
- sysdate+(5/24/60/60) 在系统时间基础上延迟5秒 sysdate+5/24/60 在系统时间基础上延迟5分钟 sysdat
- python将字符串转换成数组的方法。分享给大家供大家参考。具体实现方法如下:#------------------------------
- 本文实例讲述了python将ip地址转换成整数的方法。分享给大家供大家参考。具体分析如下:有时候我们用数据库存储ip地址时可以将ip地址转换