Effacer les filtres
Effacer les filtres

How do I plot lines with different line widths?

8 328 vues (au cours des 30 derniers jours)
Leor Greenberger
Leor Greenberger le 22 Sep 2011
I want to do:
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.
  2 commentaires
Jagannadh Kumar
Jagannadh Kumar le 8 Mar 2016
Try writing like this plot(x1,y1,'Linewidth',6,x2,y2,'Linewidth',8)
Ramesh M
Ramesh M le 28 Juil 2016
it works thnx

Connectez-vous pour commenter.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 22 Sep 2011
Modifié(e) : MathWorks Support Team le 8 Nov 2018
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.
hold on
hold off
  2 commentaires
Tyler Tomlinson
Tyler Tomlinson le 2 Déc 2015
Thank you that worked great.
Mike Garrity
Mike Garrity le 8 Mar 2016
Just FYI, there is an "official" syntax for setting a property to different values on different objects. However, it's really ugly, and doesn't work everywhere. For example, I don't think that the plot function accepts this form.
It looks like this:
h = plot(x1,y1,x2,y2);
The property name and property value need to each be a cell array, and the shape of the value cell array has to match the shape of the handle cell array.
That said, you're really better off with 2 calls to set in this case.

Connectez-vous pour commenter.

Plus de réponses (3)

Wayne King
Wayne King le 22 Sep 2011
Hi: You can use handles.
h = plot(x1,y1,x2,y2);

Hari Desanur
Hari Desanur le 15 Nov 2016
Modifié(e) : Hari Desanur le 15 Nov 2016
The line width for a particular line can be set using line object handles. For example -
l = plot(x1,y1,x2,y2);
l(1).LineWidth = 3; % set line width of 3 for the first line (x1,y1)
l(2).LineWidth = 6;

x = 1:.01:10;
y1 = sin(x);
y2 = cos(x);
p = plot(x,y1,x,y2)
Line width, specified as a positive value in points, where 1 point = 1/72 of an inch. If the line has markers, then the line width also affects the marker edges.
The line width cannot be thinner than the width of a pixel. If you set the line width to a value that is less than the width of a pixel on your system, the line displays as one pixel wide.


En savoir plus sur Formatting and Annotation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by