You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Problem with colorbar and values of scale
16 views (last 30 days)
Show older comments
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)
Image Analyst
on 8 May 2014
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
Haimon
on 8 May 2014
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
Image Analyst
on 9 May 2014
How are you so sure that rhoMin is zero? What does this say in the command window?
min(rho(:))
max(rho(:))
rhomin
rhomax
Image Analyst
on 9 May 2014
Edited: Image Analyst
on 9 May 2014
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
Image Analyst
on 9 May 2014
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).
Image Analyst
on 10 May 2014
I can't do much more until you attach your data.
Image Analyst
on 10 May 2014
Nope, sorry.
Image Analyst
on 13 May 2014
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.
Image Analyst
on 31 May 2014
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?
Image Analyst
on 31 May 2014
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.
Haimon
on 2 Jun 2014
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.
Image Analyst
on 2 Jun 2014
You must have changed the colormap.
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.
Image Analyst
on 3 Jun 2014
I give the same answer as in your duplicate question: http://www.mathworks.com/matlabcentral/answers/132156#comment_217525
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)