Colour Associated with a Point in a Colourmap
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
DGM
el 11 de Feb. de 2022
Editada: DGM
el 11 de Feb. de 2022
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 comentarios
Image Analyst
el 11 de Feb. de 2022
Editada: Image Analyst
el 11 de Feb. de 2022
@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)?
Más respuestas (1)
Image Analyst
el 10 de Feb. de 2022
See the caxis() function.
3 comentarios
DGM
el 11 de Feb. de 2022
Editada: DGM
el 11 de Feb. de 2022
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
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
el 11 de Feb. de 2022
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).
Ver también
Categorías
Más información sobre Colormaps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!