matlab - 如何从两种颜色创建内插颜色图或调色板?

标签 matlab plot colors matlab-figure colorbar

这个问题在这里已经有了答案:





How to create a custom colormap programmatically?

(2 个回答)


5年前关闭。




我想在两种颜色之间创建一个调色板。例如在 之间蓝色 红色 有 20 或 50 个实例。

这如何在 Matlab R2014b 中实现?

最佳答案

您可以使用任何类型的插值(例如 interp1 )来创建您自己的自定义 colormap两种颜色或多种颜色之间。颜色图基本上是具有 RGB 值的 3 列矩阵。就您而言,它非常简单,因为您只需要 红色[1 0 0]蓝色 [0 0 1]并在两者之间进行线性插值。 linspace 因此是最好的选择。

n = 50;               %// number of colors

R = linspace(1,0,n);  %// Red from 1 to 0
B = linspace(0,1,n);  %// Blue from 0 to 1
G = zeros(size(R));   %// Green all zero

colormap( [R(:), G(:), B(:)] );  %// create colormap

%// some example figure
figure(1)
surf(peaks)
colorbar

enter image description here

请注意,您还可以通过键入 colormapeditor 来使用颜色图 GUI。 .

替代方案您也可以使用 2D-interpolation :
n = 50;                %// number of colors

cmap(1,:) = [1 0 0];   %// color first row - red
cmap(2,:) = [0 1 0];   %// color 25th row - green
cmap(3,:) = [0 0 1];   %// color 50th row - blue

[X,Y] = meshgrid([1:3],[1:50]);  %// mesh of indices

cmap = interp2(X([1,25,50],:),Y([1,25,50],:),cmap,X,Y); %// interpolate colormap
colormap(cmap) %// set color map

%// some example figure
figure(1)
surf(peaks)
colorbar

enter image description here

还有一个使用 的例子样条插值获得更广泛的蓝色和红色区域:
n = 50;                %// number of colors

v = [0,0,0.1,0.5,0.9,1,1];
x = [-5*n,0, 0.45*n, 0.5*n, 0.55*n, n, 5*n]; 
xq = linspace(1,n,n);
vq = interp1(x,v,xq,'spline');
vq = vq - min(vq);
vq = vq./max(vq);

B = vq;  %// Blue from 0 to 1 with spline shape
R = fliplr(B);  %// Red as Blue but mirrored
G = zeros(size(R));   %// Green all zero

colormap( [R(:), G(:), B(:)] );  %// create colormap

%// some example figure
figure(1)
surf(peaks)
colorbar

enter image description here

或者使用您想要的任何数学函数:
n = 50;                %// number of colors

t = linspace(0,4*pi,50);

B = sin(t)*0.5 + 0.5;  %// Blue from 0 to 1 as sine
R = cos(t)*0.5 + 0.5;  %// Red from 0 to 1 as cosine
G = zeros(size(R));   %// Green all zero

colormap( [R(:), G(:), B(:)] );  %// create colormap

%// some example figure
figure(1)
surf(peaks)
colorbar

enter image description here

关于matlab - 如何从两种颜色创建内插颜色图或调色板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30851050/

相关文章:

function - 为什么我没有通过任何错误时会收到 "Too many input arguments"错误?

javascript - 是否可以使用plotly.js 绘制饼图?

HTML + CSS 渲染颜色

matlab - 结构文件元胞数组

matlab - 在函数声明中指定它应该应用于单个参数

image - 写入超过 256 个标签的图像

r - 如何从R中的箱形图中提取异常值

r - 单独更改 GAM 图的选项?

html - CSS中具有多种颜色渐变的文本

bash - 使用 Lua 脚本启用 bash 输出颜色