I need to assign colours to lines on a plot, related to a value for each of those lines

3 Ansichten (letzte 30 Tage)
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
  1 Kommentar
Adam
Adam am 14 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrea Monfredini
Andrea Monfredini am 14 Mär. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by