How to plot a shape and let the colors change

12 views (last 30 days)
xander
xander on 11 Nov 2014
Answered: Chad Greene on 11 Nov 2014
Hello everyone,
I made a program that loops over an [100x1] matrix called ct. So for each timestep, the [100x1] matrix is update with new values.
figure
for n = 1:timeStep;
...
...
ct=ct_new
plot ct
end
I want to visualise the values of ct as folows: Each cell of the ct matrix represents an area with a certain Diameter and Height:
D_segment = a [100x1] matrix (each segment has a different diameter) z = a [100x1] matrix are the coordinates of the heights (with steps dz).
N=100;
L=19;
dz=L/(N-1);
z=0:dz:L;
I want to plot the area's of each each segment and then color the area with value of ct. The result then should be that many squares (each with its own diameter) are plotted above each other and they should change color during the loop.
Thank you very much!
  1 Comment
xander
xander on 11 Nov 2014
I managed to get the plot:
square=0*z;
figure
for i=1:N
square(i)=rectangle('position',[-D_segment(i)/2,z(i),D_segment(i),dz]);
set(square(i), 'LineStyle', 'none')
set(square(i), 'FaceColor', [0 0 1])
hold on
end
axis('equal')
colorbar
colormap jet
caxis([0 0.7])
hold off
When i execute this commando, i get the correct form/shape that i want. However, now i want to let the facecolor change each timestep of the main loop. The values of the ct matrix vary between 0 and 0.6 0 should get the color blue, 0.6 must become red Any suggestions?

Sign in to comment.

Answers (1)

Chad Greene
Chad Greene on 11 Nov 2014
With rgbmap,
colors = rgbmap('red','blue',N);
for i=1:N
square(i)=rectangle('position',[-D_segment(i)/2,z(i),D_segment(i),dz]);
set(square(i), 'LineStyle', 'none')
set(square(i), 'FaceColor', colors(N,:))
hold on
end
Or manually, define colors as
colors([linspace(0,1,N)' linspace(1,0,N)' zeros(N,1)]);

Categories

Find more on Line Plots 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!