How to add colorbar to a regular plot?

102 views (last 30 days)
Lindsay
Lindsay on 7 Oct 2014
Commented: agung pratama on 24 Jul 2020
Hi all -- I created a regular plot (not surface) that plots 16 different data sets using a colormap in a round about way (see code below). Now, I am trying to get it to add a color bar ranging from 1-16, which represents the electrode that relates to each set of data. I am unsure how to do this as the only documentation I can find relates to surface plots.
%%Plot final SOE function %%
newDefaultColors = jet(numberOfDataSets);
set(gca, 'ColorOrder', newDefaultColors);
newColorOrder = get(gca,'ColorOrder')
plot (masker*ELEC_SPACE,n1p2amp, 'LineWidth', 1.5)%plots amplitude data
num = num2str(maskch);
xlabel('Masker Electrode (mm)')
ylabel ('ECAP Amplitude (mV)');
text(masker(respidx(maxidx))*ELEC_SPACE,n1p2amp(respidx(maxidx)),[num2str(maskch)],...
'VerticalAlignment','middle',...
'HorizontalAlignment','right',...
'FontSize',14)
hold all;
set(gca,'XLim',[1*ELEC_SPACE 16*ELEC_SPACE],'YLim',[0 0.6]);
set(gca, 'XTick',0:4*ELEC_SPACE:16*ELEC_SPACE);
Thanks!

Answers (2)

Star Strider
Star Strider on 7 Oct 2014
Just invite it!
For example:
x = linspace(0,2*pi);
n = 1:16;
ofst = repmat(n',1,length(x));
y = sin(n'*x) + ofst;
colormap(jet(16))
figure(1)
plot(x, y)
hc = colorbar
cb = linspace(1,16,16);
set(hc, 'YTick',cb, 'YTickLabel',cb)
produces:
See colorbar for details.

Image Analyst
Image Analyst on 8 Oct 2014
You may also be interested in my attached color order demo. Plus, you can use legend() which it the normal method people use to indicate plot curve color rather than a colorbar.
  3 Comments
Image Analyst
Image Analyst on 24 Jul 2020
The colorbar maps your grayscale image into a colorized, RGB image via a colormap. The colormap is an N-by-3 matrix of RGB values in the range 0-1 with the first column being the red value, the second value being the green value, and the third value being the blue value. So if you use a colormap, you can consider your gray scale image as now being an "indexed" image since every gray scale in your image will correspond to a certain row (index) in the color map that says what color to display that gray scale as. Does that make sense?
In addition, you can specify what gray level corresponds to the first color (row 1 of the colormap, or the bottom row of the colorbar) and what gray level corresponds to the last color (top color in the colorbar). You do this with the caxis() function. If you don't use caxis(), your min value of your image will be the bottom color and the max value of your image will be the top color, with the in between values linearly corresponding to the in between colors. If you do use caxis(), and values below the lowest value you specify will show up as that bottom color, and any values in your image above the high value you used in caxis() will be the top color.
For example if your image ranges from -2 to +14, and you use 256 colors in the "jet" map:
imshow(grayImage, []); % Display the image
cmap = jet(256) % Blue at bottom, red at top.
colormap(cmap); % Apply colormap
colorbar; % Show color bar beside the image.
Then if you use
caxis([0, 9]); % Blue for 0 and below, and red for 9 and above.
then any values from -2 to 0 will show up as blue and any values from 9 to 14 will show up as red.
So that's how the colormap and colorbar relate to the intensity image. Now you asked "is there any relation between color bar and mm?". The colormap applies to the intensity, not the distance in the plane of the image. So for the colormap to apply to real units of mm, your intensity of your image would have to correspond to units of mm. For example, the image could be a profilometer image of your hand. So essentially your image is a topographic image where intensity corresponds to height. So maybe you'd have a profilometer image where the heights range from -5 to +30 mm, and your gray levels are also in that range. Then if you want 0 to show up as blue, and 25 mm to show up as red, you'd use those values in caxis():
caxis([0, 25]);
agung pratama
agung pratama on 24 Jul 2020
Thanks a million, Image analysis, that's a remarkable answer.
My project is about image analysis, I want to measure a deflection of a plate from the centred load. I record with a camera to get the images of moire and fringe projected on the plate changes during the loading process.
I assume I will get images similar to this one
The problem is I don't know the deflection's height range will be. If you have any suggestion please help me, that's will be very kind of you

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!