MATLAB Answers

How do I plot lines with different line widths?

9,954 ビュー (過去 30 日間)
Leor Greenberger
Leor Greenberger 2011 年 9 月 22 日
回答済み: MASTER WHOS 2019 年 2 月 14 日
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 件のコメント

Jagannadh Kumar
Jagannadh Kumar 2016 年 3 月 8 日
Try writing like this plot(x1,y1,'Linewidth',6,x2,y2,'Linewidth',8)
Ramesh M
Ramesh M 2016 年 7 月 28 日
it works thnx

サインイン to comment.

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 22 日
編集済み: MathWorks Support Team 2018 年 11 月 8 日
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 件のコメント

Tyler Tomlinson
Tyler Tomlinson 2015 年 12 月 2 日
Thank you that worked great.
Mike Garrity
Mike Garrity 2016 年 3 月 8 日
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.

サインイン to comment.

その他の回答 (3 件)

Wayne King
Wayne King 2011 年 9 月 22 日
Hi: You can use handles.
h = plot(x1,y1,x2,y2);
set(h(1),'linewidth',1);
set(h(2),'linewidth',2);

  0 件のコメント

サインイン to comment.


Hari Desanur
Hari Desanur 2016 年 11 月 15 日
編集済み: Hari Desanur 2016 年 11 月 15 日
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 件のコメント

サインイン to comment.


MASTER WHOS
MASTER WHOS 2019 年 2 月 14 日
1-What might be the problem? 2-What does linewidth property mean? 3-What to do?
[I am using matlab R2016b]
When I want to plot line with color and certain width, I recceive an error:
THE COMMAND
plot(SNR_db,Pd_NSP_cat_mean(:,2),'b','LineWidth',2.5)
THE ERROR:
Error using plot
There is no LineWidth property on the Line class.
Error in PROJECTION_BASED_SHARING (line 76)
plot(SNR_db ,Pd_NSP_cat_mean(:,1),'g','LineWidth ',2.5)

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by