How to assign different colours to the "levels" of a cylinder

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

Are you using surf to draw the cylinder given the X,Y,Z output of cylinder2P?
Yep.
hSurface = surf(X,Y,Z);
set(hSurface,'FaceColor',[1 0 0]); % this is just to avoid a rainbow plot
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.
Thank you very much for the tip! I did not see that example when I first looked over surf . Still, I cannot recreate the matrix properly. The C matrix has to have the same size as the coordinates X, Y and Z, but the vector x, which stores the colour data, has size [1,n], with n being the number of levels of the cylinder. How should I create C ? As you can see, I'm not so prepared on 3D visualisation in Matlab. Sorry for that.
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.
No, x is not monotonically increasing. Also, no comparison between Z and x should be needed.
My x has size 14,1:
x =
8.0730
8.0730
6.6620
6.6620
9.3020
9.3020
10.0000
10.0000
10.0000
10.0000
9.7290
9.7290
1.5820
1.5820
I build my cylinder using the function cylinder2P mentioned before and a vector R specifying the radii I want:
N = length(R);
N = M;
[X, Y, Z] = cylinder2P(R, N, M, [0,0,0], [0,0,N]);
Now, what I would like to plot is a cylinder coloured at every level according to the values in x. That is, the value 10 (max(x)) will assign to level 7 (since x(7)=10) the colour red, the value 9.3 will result in colouring level 5 of a lighter red, and so on, until the value 1.58 (min(x)) will assign to the corresponding level the colour blue.
I'm sorry if I was not clear enough before.
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?
I do believe that Z defines the height/level of my cylinder. With "no comparison needed" I mean that there should be no need to compare values between Z and x, since x(1) defines the colour of Z(1,1), x(2) defines the colour of Z(2,1) and so on.
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?
Z's size is 14x14 (same as X and Y). Its structure is like:
Z =
a1 a1 a1
b1 b1 b1
c1 c1 c1
I suppose no comparison is needed, since Z defines only the height of the cylinder. Of course I may be wrong, this is why I am asking here.
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.
I did not copy here the Z matrix because of its size. Z is a square matrix 14x14 who looks like:
Z =
0 0 0 ... 0
2.3 2.3 2.3 ... 2.3
4.2 4.2 4.2 ... 4.2
5.9 5.9 5.9 ... 5.9
...
Is this the answer you were looking for?
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?
I would like to assign to the value x(1) = 8.073 a certain colour, and see level one plotted in that colour. In general, given x(n) = t , I would like level n to be plotted according to a certain colour defined by t.
I am not able to relate t (or 8.073) to a certain colour and subsequently to Z, this is what I am asking.
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;
Thank you very much! That solved my doubts. You may want to copy-paste the text as an answer, so I can give you the appropriate feedback.
Glad to have been able to help!

Sign in to comment.

 Accepted Answer

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;

More Answers (0)

Categories

Asked:

on 15 Jul 2014

Commented:

on 17 Jul 2014

Community Treasure Hunt

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

Start Hunting!