plot legend shows wrong linespec

16 ビュー (過去 30 日間)
William Rose
William Rose 2024 年 4 月 15 日
コメント済み: William Rose 2024 年 4 月 16 日
Starting with no open figures, and some data already computed, I made a plot as follows:
load('data') % I did not use this commands initially. It is here for demonstration.
figure
plot(t,molTot,'-rx',t,molGas,'-bo');
legend('a','b')
and I got a plot like the plot above, including the incorrect lines in the legend.
I had previously seen this problem in multi-panel plots made by a complicated script. I looked in the figure properties and saw nothing obvious, but I'm not a figure property expert, so I don't know exactly what to look for. Therefore I closed all open figures, before issuing the three commands above (without the 'load(...)') - to see if the bad legend occurred with a new and simpler figure. And it did. I closed the figure shown above, then repeated the three commands above, with the same result. Therefore I saved variables t, molTot, molGas in file data.mat. Then I closed and restarted Matlab. I did
load('data')
figure
plot(t,molTot,'-rx',t,molGas,'-bo');
legend('a','b')
and I got asnother figure with an incorrect linespec in the legend, as seen above.
Why is this happening? [Matlab 2023b on Windows 11 Pro.]
Thanks.

採用された回答

VBBV
VBBV 2024 年 4 月 16 日
d = load('data.mat')
d = struct with fields:
molGas: [401x1 double] molTot: 3.6118 t: [401x1 double]
%figure
plot(d.t,repmat(d.molTot,length(d.t),1),'-rx',d.t,d.molGas,'-bo');
legend('a','b')
  2 件のコメント
VBBV
VBBV 2024 年 4 月 16 日
molTot is scalar quantity while others are not
William Rose
William Rose 2024 年 4 月 16 日
Thank you @VBBV and @the cyclist.

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

その他の回答 (1 件)

the cyclist
the cyclist 2024 年 4 月 16 日
The syntax you are using created a plot with 402 "lines". The first 401 are red, and the last one is blue.
load('data') % I did not use this commands initially. It is here for demonstration.
figure
h = plot(t,molTot,'-rx',t,molGas,'-bo');
legend('a','b')
size(h)
ans = 1x2
402 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You presumably intended something more like
figure
h = plot(t,molTot*ones(size(t)),'-rx',t,molGas,'-bo');
legend('a','b')
  1 件のコメント
William Rose
William Rose 2024 年 4 月 16 日
Thank you.

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

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by