Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

matlab - How to generate a non-linear colormap/colorbar?

I would like to show a non-uniform colorbar as in the first picture. I have tried the code below. 'mycamp4' is a colormap manually saved. The result is shown as the second figure. The number 0.1 and 1.5 will be too closed too see. How can I make the colorbar like in the first picture?

v = [0.1 1 1.5 5 7.5 10 30];
v_2 = [0.1 1.5 5 7.5 10 30];
contourf(X,Y,pdf_normal',v);
h = colorbar;
load('MyColormaps','mycmap4');
set(gcf,'Colormap',mycmap4);
set(h, 'YTick', v_2)

Picture 1:

enter image description here

Picture 2:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here a step by step explanation.

First consider the following example:

[X,Y,Z1] = peaks;

figure(1)
[~,h1] = contourf(X,Y,Z1,20);
a1 = colorbar;
colormap(jet)
caxis([-6,6])

which will give you the following plot:

enter image description here

It has linear data and a linear colormap. Now I want to scale the Z-Data to get it non-linear like in your case. I chose a simple squaring of the data.

Z2 = get(h1,'ZData');
scalefactor = @(x) sign(x).*x.^2;
Z2 = scalefactor(Z2);

Thats the actual example data, similar to yours:

figure(2)
[~,h2] = contourf(X,Y,Z2,20);
a2 = colorbar;
colormap(jet)
caxis([-6^2,6^2])

enter image description here

Now we have non-linear data, but still a linear colormap and colorbar.

Until now everything was to generate example data similar to yours.


Now the actual answer:

Get the data of your plot:

Z3 = get(h2,'ZData');

and scale it with a relation you hopefully know more or less:

descalefactor = @(x) sign(x).*abs(x).^(1/2);
Z3 = descalefactor(Z3);

Plot that scaled data:

figure(3)
[~,h3] = contourf(X,Y,Z3,20);
a3 = colorbar;
colormap(jet)
caxis([-6,6])

get the Y-Ticks and scale it with the inverse function, like your data:

ticks = get(a3,'YTick');
ticks = scalefactor(ticks);

set these inversly scaled colorbar ticks:

set(a3,'YTickLabel',ticks)

and you finally get a seemingly linearized plot (but you could just backup your non-linear data from before), with a with non-linear colormap and colorbar ticks.

enter image description here

As expected, the plot looks the same like in the first the example, but with a scaled colorbar axis.

If you don't have any functional relationship, try to get one, e.g. with the curve fitting toolbox.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...