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

end*_*ith 14 matplotlib color-mapping complex-numbers heatmap

换句话说,我想制作一个热图(或曲面图),其颜色随2个变量的变化而变化.(具体来说,亮度=幅度和色调=相位.)有没有本地方法来做到这一点?类似情节的一些例子:

使用两个颜色条,一个用于幅度,一个用于相位

使用颜色条表示幅度,使用圆形图例表示相位

使用2D颜色条指示两个变量的变化

确切(?)我想要做的几个很好的例子.

来自天文学的更多例子,但具有非感性色调

Pio*_*dal 8

imshow可以采用一系列[r,g,b]条目.因此,您可以将绝对值转换为强度和阶段 - 色调.

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

因此,对于您的数据,Z您可以使用例如

imshow(complex_array_to_rgb(Z))
Run Code Online (Sandbox Code Playgroud)

在哪里(编辑:由于这个建议,它更快更好)

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
Run Code Online (Sandbox Code Playgroud)

所以,例如:

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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

imshow(complex_array_to_rgb(Z, rmax=5, theme='light'), extent=(-1,1,-1,1))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


tac*_*ell 6

imshow 将采用NxMx3(rbg)或NxMx4(grba)数组,以便您可以"手动"进行颜色映射.

您可以通过子类化来获得一些牵引力,将Normalize您的矢量映射到缩放器并非常巧妙地布置自定义颜色贴图(但我认为这最终必须将您的一个尺寸装入其中).

我做过类似的事情(pdf链接,请参见第24页的图),但代码已经存在MATLAB(并存放在我的档案中).

我同意双变量颜色贴图会很有用(主要用于表示非常密集的矢量场,无论你做什么,你都可以在小河上找到它).我认为明显的扩展是让彩色地图采用复杂的论点.它需要专门的子类NormalizeColormap,如果我认为要实现很多工作,我会来回走动.我怀疑如果你手动工作它只是一个api争论的问题.