Colour Associated with a Point in a Colourmap

2 次查看(过去 30 天)
I have made a colourplot using the pcolor command in MATLAB. However, I am trying to make a colourbar which shows roughly the range of data a single colour encompasses. I am not sure how to even start coding this, any help would be massively appreciated.
The data I am using is attached, and the following code is what I am running.
load lambda300.mat;
load delta300.mat;
load maxPF300.mat;
figure;
s = pcolor(lambda_array/1e-19,delta_array/1e-19,maxPF300);
colormap turbo(7);
colorbar('Ticks',[1,4.55,8,11.5,15,18.5,22],...
'TickLabels',{'0<σS^2<3.5','3.5<σS^2<7','7<σS^2<10.5', ...
'10.5<σS^2<14','14<σS^2<17.5', '17.5<σS^2<21', '21<σS^2<25',}, 'fontsize', 15);
shading interp;
The placing of the ticks and the values associated with them has very much been a stab in the dark. Therefore, I would like to know which colour approximately maps which range of values of the maxPF300 data, and is there an automated way for doing this.
Thank you very much!
  2 个评论
Highphi
Highphi 2022-2-10
编辑:Highphi 2022-2-10
colorbar('on')
But I think you want something a bit more in depth. Could you pass along the data you're plotting?
Tb
Tb 2022-2-10
Thank you for your response, and yes I managed to get that it's getting the ticks at the right place that's giving me issues. I have the data saved as .mat files and I will attach them with the original post, I will also add the code I am currently running @Highphi

请先登录,再进行评论。

采纳的回答

DGM
DGM 2022-2-11
编辑:DGM 2022-2-11
Here. This may be close to what you're trying to do. I don't know that it's really any clearer to do this than it is to put the tick labels on the breakpoints as I did in the prior example. This way does eat up more figure space with the long labels though.
x = 1:100;
y = x';
z = x+y;
nlevels = 7;
hp = pcolor(x,y,z); hold on
shading flat
hc = colorbar;
colormap(jet(nlevels))
clim = caxis;
bp = linspace(clim(1),clim(2),nlevels+1);
segcenter = bp(1:end-1) + range(clim)/(nlevels*2);
tlab = split(sprintf('%.2f <σS^2< %.2f\n',[bp(1:end-1); bp(2:end)]),newline);
hc.Ticks = segcenter;
hc.TickLabels = tlab;
% need to adjust the axes position to make room for the cb labels
hax = gca;
oldaxw = hax.Position(3);
hax.Position(3) = oldaxw*0.85;
  3 个评论
Image Analyst
Image Analyst 2022-2-11
编辑:Image Analyst 2022-2-11
@Tb if it's at the bottom of the color how would you know which color the range applies to? You'd have to just use tick labels that mention the first number if you're going to do it that way since the first number indicates the start of the color range and the numbers go up from there.
By the way, did you see my answer below where you can specify the range instead of just taking what you get (the default)?
Tb
Tb 2022-2-11
Yes I did, thank you for that, I have tried to use that as well, but I think the default range works well for me.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2022-2-10
See the caxis() function.
  3 个评论
DGM
DGM 2022-2-11
编辑:DGM 2022-2-11
Consider the example:
x = 1:100;
y = x';
z = x+y;
nlevels = 7;
hp = pcolor(x,y,z); hold on
shading flat
hc = colorbar;
colormap(jet(nlevels))
clim = caxis
clim = 1×2
2 200
hc.Ticks = linspace(clim(1),clim(2),nlevels+1);
To answer your question, the colorbar has 7+1 breakpoints, but the breakpoints are spaced by caxis()/7.
Image Analyst
Image Analyst 2022-2-11
You call caxis and send it the values. You didn't do that. You just got caxis and found out what the default values are. For example if you want the 7 colors to go from 30 to 150 you'd do
caxis([30, 150]);
so anything 30 or less will show up as blue, then you'd have the 7 colors for values up to 150. If your data is 150 or above, those values will show up at the top color in the colormap (red for the image you showed above).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by