gpt4 book ai didi

matplotlib - 有没有办法在 matplotlib 中使用双变量颜色图?

转载 作者:行者123 更新时间:2023-12-03 08:00:28 26 4
gpt4 key购买 nike

换句话说,我想制作一个热图(或曲面图),其中颜色作为 2 个变量的函数而变化。 (具体来说,亮度 = 幅度,色调 = 相位。)有没有本地方法可以做到这一点?
一些类似地块的例子:
uses two colorbars, one for magnitude and one for phase
uses a colorbar for magnitude and a circular legend for phase
uses a 2D colorbar to indicate the changes in both variables
Several good examples of exactly(?) what I want to do.
More examples from astronomy, but with non-perceptual hue
编辑:这是我用它做的:https://github.com/endolith/complex_colormap

最佳答案

imshow可以采用 [r, g, b] 条目的数组。因此,您可以将绝对值转换为强度和相位 - 色相。

我将使用复数作为例子,因为它最有意义。如果需要,您可以随时添加 numpy数组 Z = X + 1j * Y .

所以对于您的数据 Z你可以使用例如

imshow(complex_array_to_rgb(Z))

哪里(编辑:由于 this suggestion 使它更快更好)
def complex_array_to_rgb(X, theme='dark', rmax=None):
'''Takes an array of complex number and converts it to an array of [r, g, b],
where phase gives hue and saturaton/value are given by the absolute value.
Especially for use with imshow for complex plots.'''
absmax = rmax or np.abs(X).max()
Y = np.zeros(X.shape + (3,), dtype='float')
Y[..., 0] = np.angle(X) / (2 * pi) % 1
if theme == 'light':
Y[..., 1] = np.clip(np.abs(X) / absmax, 0, 1)
Y[..., 2] = 1
elif theme == 'dark':
Y[..., 1] = 1
Y[..., 2] = np.clip(np.abs(X) / absmax, 0, 1)
Y = matplotlib.colors.hsv_to_rgb(Y)
return Y

因此,例如:
Z = np.array([[3*(x + 1j*y)**3 + 1/(x + 1j*y)**2
for x in arange(-1,1,0.05)] for y in arange(-1,1,0.05)])
imshow(complex_array_to_rgb(Z, rmax=5), extent=(-1,1,-1,1))

enter image description here
imshow(complex_array_to_rgb(Z, rmax=5, theme='light'), extent=(-1,1,-1,1))

enter image description here

关于matplotlib - 有没有办法在 matplotlib 中使用双变量颜色图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15207255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com