How to assign different colours to the "levels" of a cylinder
Show older comments
Hello everyone,
As the title says, I need to specify different colours for the segments composing a cylinder.
I built my cylindric structure using this .m file on File Exchange:
since I needed the structure to have different radius for every level.
Now, I have a vector with some values (let's call it x). What I'd like to do is to assign some colours to these values (say, if a value is in a certain range, assign to it colour blue). Then, using the data in x, I'd like to specify for every level of the cylinder the corresponding colour information stored in x.
Hope to have been clear enough.
Any help would be appreciated.
17 Comments
Geoff Hayes
on 15 Jul 2014
Are you using surf to draw the cylinder given the X,Y,Z output of cylinder2P?
Geoff Hayes
on 15 Jul 2014
Perhaps check out the Sphere with Two Colors example from surf. You can create a C matrix which can be used to define the colours.
Vittorio
on 15 Jul 2014
Geoff Hayes
on 16 Jul 2014
Is x a monotonically increasing vector? Are you saying that if a value in Z (level/height) is less than x(1) then this value of Z gets assigned the colour that is associated with x(1)?
If so, then you could do something like
C = Z;
for k=1:length(x)
if k==1
C(C<=x(k)) = k;
else
C(C>x(k-1) & C<=x(k)) = k;
end
end
surf(X,Y,Z,C); colormap(yourColorMap);
where yourColorMap is the colour map for your n colours. The above code assumes that the x(n) is greater than or equal to the maximum value of Z.
Vittorio
on 16 Jul 2014
Geoff Hayes
on 16 Jul 2014
Given the X, Y and Z data returned from cylinder2P, what do you consider to be the level? You have indicated that it is not Z, so is it a value from X or Y?
Vittorio
on 16 Jul 2014
Geoff Hayes
on 16 Jul 2014
Have you verified that no comparison is needed? What happens if a value of Z is 2.3 - where does that fit into x? What are the dimensions of Z? Is it 14xn?
Vittorio
on 16 Jul 2014
Geoff Hayes
on 16 Jul 2014
But only you know what the values are for each element in Z. Is a1 equal to 8.0730? Is a3 equal to 6.6620? etc.
Vittorio
on 16 Jul 2014
Geoff Hayes
on 16 Jul 2014
Yes. So how does Z(1,1), or 0, relate to x? You have already stated that x(1) defines the colour of Z(1,1), so do you still believe this to be true given that x(1) is 8.073?
Vittorio
on 16 Jul 2014
Geoff Hayes
on 16 Jul 2014
Are the numbers in x just random, or do they mean something? If they mean something, then how do you not know how to relate them to Z?
If you are trying to associate levels to colours, then with your 14 levels, you could define a 14x3 matrix where each row is a colour given by an RGB value
colours = zeros(14,3);
colours(1,:) = [1 0 0]; % red
colours(2,:) = [0 1 0]; % green
colours(3,:) = [0 0 1]; % blue
colours(4,:) = [0.6784 0.8471 0.9020]; % light blue
etc.
The above is an example only. Now initialize C in such a way that each element is assigned a value from 1 through 14 (for each of the 14 levels) given the contents of Z. In your example, your first row of Z is all zeros. If all these correspond to the first level, then set
C(1,:) = 1;
In the second row of Z, all values are 2.3. If this corresponds to the second level, then set
C(2,:) = 2;
Do this for all rows of C.
Now to display the cylinder with the specified colours, do something like
figure;
surf(X,Y,Z,C);
colormap(colours);
colorbar;
Vittorio
on 17 Jul 2014
Geoff Hayes
on 17 Jul 2014
Glad to have been able to help!
Accepted Answer
More Answers (0)
Categories
Find more on Color and Styling 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!