我想显示一个颜色梯度作为matplotlib图表的背景。 我发现some code on the matplotlib site接近我想要的
但是在使用这个示例并创建自己的颜色映射时,我指定的值与轴上的值不匹配
例如,使用上面的示例代码和。。。 在dict中指定我的颜色和值:
注意间隔值的0.8,我希望0-0.8是黑色,0.8到1.0是红色到黑色的渐变。
cdict1 = {
'red':
[
(0.0, 0.0, 0.0),
(0.8, 0.0, 0.0),
(0.8, 1.0, 1.0),
(1.0, 0.0, 0.0)
],
'blue': [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)],
'green': [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)]
}
将dict转换为颜色映射:
from matplotlib.colors import LinearSegmentedColormap
testcm1 = LinearSegmentedColormap('testcm1 ', cdict1)
更改gradient_image
的调用以使用我的颜色映射:
gradient_image(ax, direction=0, extent=(0, 1, 0, 1), transform=ax.transAxes,
cmap=testcm1 , cmap_range=(0, 1))
但是我的输出显示红黑梯度大约从0.72开始。
当然,我希望梯度正好匹配y轴上的0.8值,我不知道这里发生了什么
我的代码:
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
np.random.seed(19680801)
def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
"""
Draw a gradient image based on a colormap.
Parameters
----------
ax : Axes
The axes to draw on.
extent
The extent of the image as (xmin, xmax, ymin, ymax).
By default, this is in Axes coordinates but may be
changed using the *transform* kwarg.
direction : float
The direction of the gradient. This is a number in
range 0 (=vertical) to 1 (=horizontal).
cmap_range : float, float
The fraction (cmin, cmax) of the colormap that should be
used for the gradient, where the complete colormap is (0, 1).
**kwargs
Other parameters are passed on to `.Axes.imshow()`.
In particular useful is *cmap*.
"""
phi = direction * np.pi / 2
v = np.array([np.cos(phi), np.sin(phi)])
X = np.array([[v @ [1, 0], v @ [1, 1]],
[v @ [0, 0], v @ [0, 1]]])
a, b = cmap_range
X = a + (b - a) / X.max() * X
im = ax.imshow(X, extent=extent, interpolation='bicubic',
vmin=0, vmax=1, **kwargs)
return im
xmin, xmax = xlim = 0, 10
ymin, ymax = ylim = 0, 1
fig, ax = plt.subplots()
ax.set(xlim=xlim, ylim=ylim, autoscale_on=False)
cdict1 = {
'red':
[
(0.0, 0.0, 0.0),
(0.8, 0.0, 0.0),
(0.8, 1.0, 1.0),
(1.0, 0.0, 0.0)
],
'blue': [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)],
'green': [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)]
}
testcm1 = LinearSegmentedColormap('testcm1', cdict1)
# background image
gradient_image(ax, direction=0, extent=(0, 1, 0, 1), transform=ax.transAxes,
cmap=testcm1, cmap_range=(0, 1))
ax.set_aspect('auto')
plt.show()
首先,我想说只有三个定位点,所以颜色字典应该看起来像
但这不是真正的问题
真正的问题是你的图像由4个角点组成。双三次插值会产生梯度;当然,你没有足够的分辨率来显示指定的精确梯度。特别是,0.8不是图像数据的一部分
因此,需要定义一个具有足够分辨率的图像来显示不连续性(在下面的文章中,我还使colormap的创建更加直观。)
相关问题 更多 >
编程相关推荐