Python中的 Matplotlib.colors.Colormap 类
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
matplotlib.colors.Colormap
matplotlib.colors.Colormap类属于matplotlib.colors模块。 matplotlib.colors 模块用于将颜色或数字参数转换为 RGBA 或 RGB。该模块用于将数字映射到颜色或在一维颜色数组(也称为颜色图)中进行颜色规范转换。
matplotlib.colors.Colormap 类是所有标量到 RGBA 映射的基类。通常,颜色图实例用于将数据值(浮点数)从区间 0-1 转换为它们各自的 RGBA 颜色。这里 matplotlib.colors.Normalize 类用于缩放数据。 matplotlib.cm.ScalarMappable 子类大量使用它来进行数据->标准化->映射到颜色处理链。
句法:
class matplotlib.colors.Colormap(name, N=256)
Parameters:
- name: It accepts a string that represents the name of the color.
- N: It is an integer value that represents the number of rgb quantization levels.
上课方法:
- colorbar_extend = None :如果颜色图存在于标量可映射对象上并且 colorbar_extend 设置为 false,则 colorbar_extend 将通过颜色条创建作为 matplotlib.colorbar.Colorbar 的构造函数中的扩展关键字的默认值。
- is_gray(self):返回一个布尔值以检查 plt 是否为灰色。
- reversed(self, name=None) :用于制作颜色图的反转实例。此函数未针对基类实现。它有一个参数,即可选的名称,并且接受反转颜色图的字符串名称。如果设置为 None,则成为父颜色图的名称 +“r”。
- set_bad(self, color='k', alpha=None):设置用于掩码值的颜色。
- set_over(self, color='k',, alpha=None):用于设置颜色以用于高超出范围的值。它要求 norm.clip 为 False。
- set_under(self, color='k',, alpha=None):用于设置颜色以用于低超出范围的值。它要求 norm.clip 为 False。
示例:
import numpy as np
import matplotlib.pyplot as plt
start_point = 'lower'
diff = 0.025
a = b = np.arange(-3.0, 3.01, diff)
A, B = np.meshgrid(a, b)
X1 = np.exp(-A**2 - B**2)
X2 = np.exp(-(A - 1)**2 - (B - 1)**2)
X = (X1 - X2) * 2
RR, RC = X.shape
# putting NaNs in one corner:
X[-RR // 6:, -RC // 6:] = np.nan
X = np.ma.array(X)
# masking the other corner:
X[:RR // 6, :RC // 6] = np.ma.masked
# masking a circle in the middle:
INNER = np.sqrt(A**2 + B**2) < 0.5
X[INNER] = np.ma.masked
# using automatic selection of
# contour levels;
figure1, axes2 = plt.subplots(constrained_layout = True)
C = axes2.contourf(A, B, X, 10,
cmap = plt.cm.bone,
origin = start_point)
C2 = axes2.contour(C, levels = C.levels[::2],
colors ='r', origin = start_point)
axes2.set_title('3 masked regions')
axes2.set_xlabel('length of word anomaly')
axes2.set_ylabel('length of sentence anomaly')
# Make a colorbar for the ContourSet
# returned by the contourf call.
cbar = figure1.colorbar(C)
cbar.ax.set_ylabel('coefficient of verbosity')
# Add the contour line levels
# to the colorbar
cbar.add_lines(C2)
figure2, axes2 = plt.subplots(constrained_layout = True)
# making a contour plot with the
# levels specified,
levels = [-1.5, -1, -0.5, 0, 0.5, 1]
C3 = axes2.contourf(A, B, X, levels,
colors =('r', 'g', 'b'),
origin = start_point,
extend ='both')
# data below the lowest contour
# level yellow, data below the
# highest level green:
C3.cmap.set_under('yellow')
C3.cmap.set_over('green')
C4 = axes2.contour(A, B, X, levels,
colors =('k', ),
linewidths =(3, ),
origin = start_point)
axes2.set_title('Listed colors (3 masked regions)')
axes2.clabel(C4, fmt ='% 2.1f',
colors ='w',
fontsize = 14)
figure2.colorbar(C3)
# Illustrating all 4 possible
# "extend" settings:
extends = ["neither", "both", "min", "max"]
cmap = plt.cm.get_cmap("winter")
cmap.set_under("green")
cmap.set_over("red")
figure, axes = plt.subplots(2, 2,
constrained_layout = True)
for ax, extend in zip(axes.ravel(), extends):
cs = ax.contourf(A, B, X, levels,
cmap = cmap,
extend = extend,
origin = start_point)
figure.colorbar(cs, ax = ax, shrink = 0.9)
ax.set_title("extend = % s" % extend)
ax.locator_params(nbins = 4)
plt.show()
输出:
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。