I need to assign colours to lines on a plot, related to a value for each of those lines
5 次查看(过去 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.![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 个评论
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!