display legends with different line styles in matlab

51 ビュー (過去 30 日間)
user1234
user1234 2017 年 5 月 7 日
コメント済み: Samuel Suakye 2022 年 10 月 24 日
How one can draw a graph with different line styles corresponding to different legends? For instance, my data is 5 by 4 as follows:
__________________________________________________
A B C D
__________________________________________________
0.84702 0.64419 0.61728 0.48538
0.76445 0.1864 0.39728 0.15959
0.04743 0.0412 0.09432 0.86721
0.4256 0.1458 0.05432 0.803192
0.9855 0.8242 0.5679 0.7102
___________________________________________________
So far I tried the following, first I saved this data file as 'myData.m'. Next, I load it on my Matlab window using the command 'load myData.m'. Then after, I have used the followig codes:
[n,p] = size(myData);
t = 1:n;
plot(t,myData)
legend('A','B','C','D','Location','NorthEast')
xlabel('Time'); ylabel('Value');
How can I display legends with different line styles((like '-','.-','.' and '--')) along with different colours using Matlab?

採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 7 日
If you plot() one line at a time, then you can usually pass a linespec to the plotting routines; sometimes you need to pass a 'LineStyle' name/value pair.
If you are plot()'ing with four different X, Y pairs, then you can put a linespec after each one:
plot(x1, y1, '-', x2, y2, '.', x3, y3, '--', x4, y4, '.-')
If you are plotting with four columns in your Y, then you need to record the line handles and set their LineStyle property:
h = plot(x, Y_by_4);
set(h(1), 'LineStyle', '-');
set(h(2), 'LineStyle', '.');
etc
  2 件のコメント
user1234
user1234 2017 年 5 月 7 日
@Walter Roberson Thanks! I edited it now. Still I couldn't manage the error.
Walter Roberson
Walter Roberson 2017 年 5 月 7 日
Replace
[n,p] = size(myData);
t = 1:n;
plot(t,myData)
with
h = plot(myData);
set(h(1), 'LineStyle', '-');
set(h(2), 'LineStyle', ':');
set(h(3), 'LineStyle', '--');
set(h(4), 'LineStyle', '-.');
Note: these are the only four valid line styles (other than 'none')

サインインしてコメントする。

その他の回答 (1 件)

Samuel Suakye
Samuel Suakye 2022 年 10 月 23 日
col = [0,0,1;0.9,0.4,0.17;0,1,0]; % Blue, Orange, Green How do I change this to linestyle
  8 件のコメント
Walter Roberson
Walter Roberson 2022 年 10 月 24 日
Change
col = [0,0,1;0.9,0.4,0.17;0,1,0]; % Blue, Orange, Green
to
col = {'-', ':', '--'};
Change
plot(linAx,W*1e-12,JxL*1e13,'color',col(i,:))
to
plot(linAx,W*1e-12,JxL*1e13, col{i});
Samuel Suakye
Samuel Suakye 2022 年 10 月 24 日
Thank you

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by