フィルターのクリア

Why don't the markers have the correct color in the legend?

2 ビュー (過去 30 日間)
Laura Gianolio
Laura Gianolio 2023 年 6 月 8 日
コメント済み: Laura Gianolio 2023 年 6 月 13 日
If you run this code you will understand what I mean by my question.
I would like that in the legend also the circles had the correct color, otherwise I would rather remove them altogether, leaving only the names.
A1 = randi(10, [5 5]);
B1 = randi(10, [5 5]);
A2 = randi(10, [5 5]);
B2 = randi(10, [5 5]);
scatter(A1,B1,'g');
hold on
grid on
scatter(A2,B2,'b');
title('TEST');
xlabel('X','FontSize',8,'FontWeight','bold');
ylabel('Y','FontSize',8','FontWeight','bold');
legend({'\color{green}G1','\color{blue}G2'},'Location','northwest');
savefig('./Figures/Test')
Error using save
Cannot create 'Test.fig' because './Figures' does not exist.

Error in matlab.graphics.internal.figfile.FigFile/write (line 33)
save(obj.Path, obj.MatVersion, '-struct', 'SaveVars');

Error in savefig (line 83)
FF.write();

採用された回答

Les Beckham
Les Beckham 2023 年 6 月 8 日
編集済み: Les Beckham 2023 年 6 月 8 日
The problem is that each of your calls to scatter is creating five separate sets of data for a total of 10 sets. But, you only request the legend to be displayed for the first two.
From the documentation:
  • To plot multiple sets of coordinates on the same set of axes, specify at least one of x or y as a matrix.
To illustrate what I mean, here is your example, but displaying the legend for all of the sets of data:
A1 = randi(10, [5 5]);
B1 = randi(10, [5 5]);
A2 = randi(10, [5 5]);
B2 = randi(10, [5 5]);
scatter(A1,B1,'g');
hold on
grid on
scatter(A2,B2,'b');
title('TEST');
xlabel('X','FontSize',8,'FontWeight','bold');
ylabel('Y','FontSize',8','FontWeight','bold');
legend
Depending on what you really are trying to do, you might just need to reshape or redefine your data as vectors instead of matrices.

その他の回答 (1 件)

VBBV
VBBV 2023 年 6 月 8 日
編集済み: VBBV 2023 年 6 月 8 日
A1 = randi(10, [5 5]);
B1 = randi(10, [5 5]);
A2 = randi(10, [5 5]);
B2 = randi(10, [5 5]);
h1=scatter(A1,B1,'g');
hold on
grid on
h2=scatter(A2,B2,'b');
title('TEST');
xlabel('X','FontSize',8,'FontWeight','bold');
ylabel('Y','FontSize',8','FontWeight','bold');
legend([h1(1),h2(1)],{'G1','G2'},'Location','northwest');
  6 件のコメント
VBBV
VBBV 2023 年 6 月 13 日
@Laura Gianolio, Do you mean to display legend with the empty markers but still having the text colored ii.e. green and blue ?
Laura Gianolio
Laura Gianolio 2023 年 6 月 13 日
Exactly, that's what I wanted to say! Because for each dataset I use markers of different shape (to distinguish between different secondary datasets) but of the same color, so I want to identify the datasets in the legend only with the names of the right color.

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

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by