The code below
(i) does not produce an error message about not matching x and y; it can be explained by implicit expansion in plot();
(ii) produces 100 legends; this I cannot explain.
x=1:100;
y=1;
plot(x,y,'DisplayName','a bug'),
legend('-DynamicLegend'),

 採用された回答

Steven Lord
Steven Lord 2019 年 9 月 11 日

0 投票

If you call plot with an output argument to return handles to any lines that are created, you'll see that 100 individual lines get created, one per point.
x=1:100;
y=1;
h = plot(x,y,'DisplayName','a bug');
size(h)
That's also why your legend has 100 entries, one per line that plot created.
If you're using release R2018b or later and you want to display a horizontal line stretching the whole width of the axes, use yline.

3 件のコメント

G A
G A 2019 年 9 月 11 日
編集済み: G A 2019 年 9 月 11 日
You can use yline within the existing axes, which are defined automatically in my case by plot(x,y). How would you use yline instead the following?
plot(x,y*ones(size(x)),'DisplayName','leg')
Steven Lord
Steven Lord 2019 年 9 月 11 日
I'm not completely sure I understand what you're asking in your first sentence. If you want to know how to use yline with the DisplayName property, that's easy.
axis([1 10 1 10]);
xline(7, 'DisplayName', 'x = 7', 'Color', 'r');
yline(pi, 'DisplayName', 'y = pi', 'Color', 'k');
legend show
Or do you want a line with markers at certain x values and/or only spanning part of the axes? If so yline isn't the right tool for that job. For that you would do what you described:
x = 1:10;
y = ones(size(x));
h = plot(x, y, ':d', 'DisplayName', 'Dotted line with diamonds');
legend show
G A
G A 2019 年 9 月 11 日
Sorry for my English. In the first sentence I was asking the following:
xlim([x(1) x(end)]);
yline(pi, 'DisplayName', 'y = pi', 'Color', 'k');
legend show
y-axis is defined automatically.

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2019 年 9 月 11 日
編集済み: Bruno Luong 2019 年 9 月 11 日

1 投票

No it's not a bug: from PLOT doc
  • If one of X or Y is a scalar and the other is either a scalar or a vector, then the plot function plots discrete points. However, to see the points you must specify a marker symbol, for example, plot(X,Y,'o').
You get really 100 points plotted.

カテゴリ

製品

リリース

R2018b

質問済み:

G A
2019 年 9 月 11 日

コメント済み:

G A
2019 年 9 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by