Problem with colorbar and values of scale

I have this image in HSV colorbar, regarding a density parameter.
Values range from 0 (air) to a maximum of 10, which was my prerrogative (it only goes up to 7.5 though).
What I want to know is why the scale starts at 0.5, and not 0? Air we consider of zero density (although it is not true, for interaction with X-rays we can consider it so)
Before someone asks, I already verified the matrix of the density variable and there is zeros around the sample, which appears in the image.
My code below:
figure;
image(rho);
colormap(hsv(ceil(rhomax-rhomin)));
cbh = colorbar('location', 'SouthOutside');
set(cbh, 'Units', 'normal', 'Xtick',0:0.5:10); %(I thought this part would make the values start from 0)
text(3.5, -1, 'Densidade (g/cm³)', 'Parent', cbh);

Answers (1)

Let's say that rho ranged from 0.7 to 7.5. So rhoMax-rhoMin = 7.5-.5 = 7. So you will have 7 color steps when hsv(7) returns. And that's what you have: 7 color steps. It makes those steps (i.e. the tick mark labels) go from rhoMax to rhoMax, which is the range in your image.. If your rho went from 1001 to 1009, you'd have 8 color steps (1009 - 1001( and the tick marks would go from 1001 to 1009 because that's what's in your image. Does that make sense?

23 Comments

It does make sense. But even then, wouldn't the program start from the minimum value, go to the maximum and divide in equal steps? The minimum value is 0, so it should start from 0 right?
Would you suggest something different?
My objective would be to show all values of density (rho). It definitely starts from 0, but the maximum value might change depending on what sample I'm working on. the sample I use
How are you so sure that rhoMin is zero? What does this say in the command window?
min(rho(:))
max(rho(:))
rhomin
rhomax
I'm sure because I verified it myself.
And this suggestion of yours is already on my code.
The values showed are 0 and 6.9997
I don't have your data so I can't verify that. All I can do is generate some sample data in the same range, and it works fine. What does this do for you:
rho = 7 * rand(700,700);
min(rho(:))
max(rho(:))
imshow(rho, []);
colormap(hsv(7));
colorbar
Answers are: ans =
4.3576e-005
ans =
7.0000
and an image with colorbar ranging from 1 to 6, although the extremes seem to be 0 and 7, without actually showing values.
OK, so if your min is near zero, the bottom of the colorbar will be 0 and top of the color bar will be 7. Keep in mind that there does not always seem to be a tick mark at the very ends. So in your case, the min of your data must be near 0.5 and the max near 7.5 (since your tick marks seem to be at the ends).
I used the min and max, again, in rho and still shows me that I have rhomin = 0, so I still can't figure out why the lowest value is 0.5, and not 0.
I can't do much more until you attach your data.
It is almost 21 mgb, so I can't attach here. Would you have an e-mail so I can send it to you?
I can't attach here, but you can get on my dropbox folder.
Here is the link:
https://www.dropbox.com/sh/4v66ly0m0ciezj2/AAC1TTHDDEO_zG1Slmr6ZpBja
Were you able to see it?
No - extremely swamped with my regular job. Maybe on the weekend... Reply to this so I'll see you as the latest poster then. If I'm the last one to reply in a post, I normally don't open it up again.
Ok. No problem.
I downloaded your data. Here's my code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
load('C:\Users\Mark\Documents\Temporary\rho.mat');
minValue = min(rho(:));
maxValue = max(rho(:));
fprintf('The min rho value = %f.\nThe max rho value = %f.\n', ...
minValue, maxValue);
imshow(rho, []);
colormap(hsv(7));
cbh = colorbar('location', 'SouthOutside');
set(cbh, 'Units', 'normal', 'Xtick',0:0.5:10); %(I thought this part would make the values start from 0)
text(3.5, -1, 'Densidade (g/cm³)', 'Parent', cbh);
Here's my image:
Do you see any problem with it?
I did that using imshow while i was waiting, and it also worked.
But I tried again with figure, and it didn't. It is pretty much like the old one.
Do you know why, considering the matrix is the same one I sent you?
It's not figure. It seems to be image(). image() causes the colorbar to start incorrectly at 0.5, while imshow() does it correctly and starts it at 0. I don't know why.
I see. That's strange, indeed.
One last thing: after I used imshow, I got this image
But if I tried to save it automatically from the data, I get this:
Why they seem to be different? I wanted to save them - and I cannot save one by one, because there are thousands of images - , so I can make a 3D model later, but if they are different, the colorbar will not be the same, and I wouldn't know how to get the colorbar from the second one - although it would be really be best if they were the same.
You must have changed the colormap.
Haimon
Haimon on 3 Jun 2014
Edited: Haimon on 3 Jun 2014
The part of the code with imshow is this:
imshow(rho, []);
colormap(hsv(rhomax-rhomin));
cbh = colorbar('location', 'SouthOutside');
set(cbh, 'Units', 'normal', 'Xtick',0:0.5:10);
text(3.5, -1, 'Densidade (g/cm³)', 'Parent', cbh);
and the next line is this:
r = sprintf('rho%04d.tif',d);
imwrite(rho,hsv((ceil(rhomax-rhomin))),r,'tif')
They are one after the other so there isn't anything between those two parts, or variables, or code, that change the colormap. Also I got those two from the same original variable, in the same run, so I also did not change my initial variable.

Sign in to comment.

Asked:

on 8 May 2014

Commented:

on 3 Jun 2014

Community Treasure Hunt

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

Start Hunting!