# Plotting multipe lines with different width

4 views (last 30 days)
Tchilabalo on 29 Sep 2019
Edited: dpb on 29 Sep 2019
I have 5000 lines with their endpoints coordinnates. I want to plot all these lines in one figure, but each line will have a different width. With a for loop (see code), it is taking forever to execute. Ia m wondering if there is a better way to do this. Thanks.
A =[X1(:) X2(:)].'; B =[Y1(:) Y2(:)].';% Line endpoints
width=round((width*5)/(max(width)))% Vector giving each line thickness
figure;hold on
for i=1:length(A);
plot(A,B,'LineWidth',width(i));
end
grid on

dpb on 29 Sep 2019
Edited: dpb on 29 Sep 2019
Firstly, your time problem is that your A and B are the vectors of all points and you're plotting the whole thing every time by referencing the arrays without subscripting. So you have length(A)^2 lines instead of just length(A).
A =[X1(:) X2(:)].'; B =[Y1(:) Y2(:)].';
w=round((width*5)/(max(width))); % width isn't defined here on RHS????
figure
hL=plot(A,B); % plot array by column, save line handle array
set(hL,{'LineWidth'},num2cell(w(:))) % set line widths for all handles from w array
As noted, there needs be a definition of the variable width on RHS of the above...

#### 1 Comment

Tchilabalo on 29 Sep 2019
Thanks!