Python中用哪些方法查看matplotlib中的colormap(cmap)的类型
Admin 2022-12-20 群英技术资讯 601 次浏览
代码如下:
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' """
使用的话: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
比如查看:[‘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'])
运行后:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了pandas如何统计某一列或某一行的缺失值数目,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
冒泡排序是一种简单的排序算法,它也是一种稳定排序算法。其实现原理是重复扫描待排序序列,并比较每一对相邻的元素,当该对元素顺序不正确时进行交换。一直重复这个过程,直到没有任何两个相邻元素可以交换,就表明完成了排序
本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于CPython的相关问题,CPython的垃圾收集器是Python内置的为了解决循环引用问题的方法,下面一起来看一下,希望对大家有帮助。
python如何实现单因素分析线性拟合及地理编码?下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
当使用桌面应用程序的时候,有没有那么一瞬间,想学习一下桌面应用程序开发?下面这篇文章主要给大家介绍了关于Python使用pywebview开发桌面应用的相关资料,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008