How do I plot lines with different line widths in matlab

Illustration
treffic - 2023-06-27T10:23:06+00:00
Question: How do I plot lines with different line widths in matlab

Hi, I want to do: plot(x1,y1,x2,y2,'LineWidth',8) but the linewidth propery ends up applying to both lines. Do I have to use two plot functions with a hold on command to have line1 a different width than line2? Thanks.

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

To plot two lines with different line widths, you can use either of these approaches.
1. Return the two “Line” objects as an output argument from the “plot” function and then set the “LineWidth” property for each.
 p = plot(x1,y1,x2,y2)
 p(1).LineWidth = 5;
 p(2).LineWidth = 10; 
2. Use the “hold on” command to plot the two lines separately. Specify the line width by setting the “LineWidth” property a name-value pair.
 
plot(x1,y1,'LineWidth',5)
 hold on
 plot(x2,y2,'LineWidth',10)
 hold off


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!