I need to assign colours to lines on a plot, related to a value for each of those lines
5 views (last 30 days)
Show older comments
Brian Dowling
on 14 Mar 2019
Answered: Andrea Monfredini
on 14 Mar 2019
in the picture below, I have a basic representation of a truss. I also have a 21x1 array of the forces in each bar... I want to find the max of this array and assign the colour red to it, the colour blue to the minimum and then for all my force values in betwen, I want to have essentially a colour map. I can find the min max values without a problem. I am stumped once I try to assign colours. Any help greatly appreciated.![Screenshot 2019-03-14 at 15.12.51.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/208498/Screenshot%202019-03-14%20at%2015.12.51.png)
![Screenshot 2019-03-14 at 15.12.51.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/208498/Screenshot%202019-03-14%20at%2015.12.51.png)
1 Comment
Adam
on 14 Mar 2019
You can create a colourmap of any size from one of the builtin colourmaps, e.g.
colours = jet( 21 );
Then, having sorted your lines according to force you can use this ordering to index into colours and set the line colour of each accordingly.
You can do it all in one instruction probably if you already have the sorted information, but I don't remember the syntax off the top of my head, but the 'Color' property of your line is what you want to change to e.g.
colours( 7, : )
for the 7th ranked line according to force.
Accepted Answer
Andrea Monfredini
on 14 Mar 2019
Hi Brian,
why don't you try with something like this?
forces = rand(21,1);
n = length(forces);
colormap = [0:1:(n-1); zeros(1,n); (n-1):-1:0]'/(n-1);
[sorted_forces, indexes] = sort(forces);
colors = zeros(n,3);
colors(indexes,:) = colormap;
for i = 1:n
h = plot([i i],[0 forces(i)],'-o', 'LineWidth',3);
hold on
set(h, {'color'}, {colors(i,:)});
end
hold off
instead of this random demo plot command
h = plot([i i],[0 forces(i)],'-o', 'LineWidth',3);
you have the plot command for the trusses, assuming that that you plot each element individually.
0 Comments
More Answers (0)
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!