How do I plot lines with different line widths? (2024)

6,888 views (last 30 days)

Show older comments

Leor Greenberger on 22 Sep 2011

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths

Answered: SHAILENDRA PANDEY on 11 Oct 2020

Accepted Answer: Fangjun Jiang

Open in MATLAB Online

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.

2 Comments

Show NoneHide None

Jagannadh Kumar on 8 Mar 2016

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#comment_348787

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#comment_348787

Try writing like this plot(x1,y1,'Linewidth',6,x2,y2,'Linewidth',8)

Ramesh M on 28 Jul 2016

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#comment_382048

it works thnx

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Fangjun Jiang on 22 Sep 2011

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_22266

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_22266

Edited: MathWorks Support Team on 8 Nov 2018

Open in MATLAB Online

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

2 Comments

Show NoneHide None

Tyler Tomlinson on 2 Dec 2015

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#comment_326933

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#comment_326933

Thank you that worked great.

Mike Garrity on 8 Mar 2016

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#comment_348799

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#comment_348799

Open in MATLAB Online

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);

set(h,{'LineWidth'},{5;10})

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.

Sign in to comment.

More Answers (3)

Wayne King on 22 Sep 2011

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_22267

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_22267

Open in MATLAB Online

Hi: You can use handles.

h = plot(x1,y1,x2,y2);

set(h(1),'linewidth',1);

set(h(2),'linewidth',2);

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Hari Desanur on 15 Nov 2016

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_243422

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_243422

Edited: Hari Desanur on 15 Nov 2016

Open in MATLAB Online

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;

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

SHAILENDRA PANDEY on 11 Oct 2020

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_510681

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/16477-how-do-i-plot-lines-with-different-line-widths#answer_510681

Open in MATLAB Online

x = 1:.01:10;

y1 = sin(x);

y2 = cos(x);

p = plot(x,y1,x,y2)

set(p,{'LineWidth'},{5;10})

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.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and Annotation

Find more on Formatting and Annotation in Help Center and File Exchange

Tags

  • plot linewidth
  • cheat sheets

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How do I plot lines with different line widths? (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How do I plot lines with different line widths? (2024)

References

Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6300

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.