I need to assign colours to lines on a plot, related to a value for each of those lines
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 1
Adam
2019년 3월 14일
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.
채택된 답변
Andrea Monfredini
2019년 3월 14일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!