如何在颜色栏中使用z值生成线性颜色图(cplot)

问题描述 投票:1回答:2

MATLAB具有cplot.m,它可以生成彩色图,基本上看起来像2d图,其中第3轴(z轴)值为色条。我可以使用任何工具/绘图技术来生成Python或IDL编程语言中的相似图吗?堆栈溢出的先前问题是处理链接中给出的其他问题。

cplot image (Linear colored line plot

python plot idl-programming-language
2个回答
1
投票

Matplotlib没有直接的cplot等价物,但是您可以使用LineCollection

有了这种了解,您必须修改通常的样板并添加特定的导入

In [1]: import numpy as np 
   ...: import matplotlib.pyplot as plt 
   ...: from matplotlib.collections import LineCollection                                 

现在,生成一些数据(c是与(x, y)点相关的第三个值)

In [2]: x = np.linspace(0, 6.3, 64) 
   ...: y = np.sin(x) ; c = np.cos(x)                                                     

[LineCollection需要一个3D数组,即段列表,每个段一个点列表,每个点一个坐标列表,我们使用此recipe构建

In [3]: points = np.array([x, y]).T.reshape(-1,1,2) 
   ...: segments = np.concatenate([points[:-1], points[1:]], axis=1)   

现在我们实例化LineCollection,指定所需的颜色图和线宽,并在告诉实例后立即将其array(映射到颜色)是数组c

In [4]: lc = LineCollection(segments, cmap='plasma', linewidth=3) 
   ...: lc.set_array(c)                                                                   

并且最终我们以自己的方式绘制lc,请调用autoscale,因为它是必需的(尝试不调用它……)并添加一个色条。

In [5]: fig, ax = plt.subplots()                                                          
   ...: ax.add_collection(lc) 
   ...: ax.autoscale() 
   ...: plt.colorbar(lc);

enter image description here

我知道,这有点笨拙,但可以。


0
投票

IDL v8对于PLOT函数VERT_COLORS具有易于使用的关键字。我在这里使用完整的彩虹:

; generate some sample data
x = cos(dindgen(100)/20)
y = sin(dindgen(100)/20)

; plot the data
p = plot(x, y, vert_colors=colortable(39, /transpose, ncolors=n_elements(y)), xrange=[-2,2], yrange=[-2,2], thick=3, /aspect_ratio)

plot data

您只需要将z轴数据缩放为RGB矢量(对于每个数据点)。

© www.soinside.com 2019 - 2024. All rights reserved.