Compact way to plot data with relative colors and legend

2 ビュー (過去 30 日間)
Sim
Sim 2022 年 10 月 12 日
コメント済み: Sim 2022 年 10 月 12 日
Is there any way to plot data with relative colors and legend in a more compact way than this one ?
Note: to remove duplicates in the legend I have used the legendUnq function.
% Input
a = {'marathon', 1, 4;
'bank holiday', 3, 6;
'bank holiday', 2, 1;
'concert', 0, 4;
'regatta', 1, 9;
'regatta', 4, 5;
'regatta', 1, 0;
'local fair', 6, 3}
% Non-compact output (and plot)
number_of_colors = length(unique(a(:,1)));
colors = jet(number_of_colors);
[~,~,idx] = unique(a(:,1));
hold on
for i = 1 : length(a)
plot(cell2mat(a(i,2)),cell2mat(a(i,3)),'o',...
'markersize',10,...
'markerfacecolor',colors(idx(i),:),...
'markeredgecolor',colors(idx(i),:),...
'DisplayName',a{i,1});
end
legend(legendUnq())

採用された回答

Davide Masiello
Davide Masiello 2022 年 10 月 12 日
編集済み: Davide Masiello 2022 年 10 月 12 日
a = {'marathon', 1, 4;
'bank holiday', 3, 6;
'bank holiday', 2, 1;
'concert', 0, 4;
'regatta', 1, 9;
'regatta', 4, 5;
'regatta', 1, 0;
'local fair', 6, 3};
[u,v,z] = unique(a(:,1));
clrs = jet(length(u));
gscatter([a{:,2}],[a{:,3}],string(a(:,1)),clrs,'.',30)

その他の回答 (1 件)

Chunru
Chunru 2022 年 10 月 12 日
% Input
a = {'marathon', 1, 4;
'bank holiday', 3, 6;
'bank holiday', 2, 1;
'concert', 0, 4;
'regatta', 1, 9;
'regatta', 4, 5;
'regatta', 1, 0;
'local fair', 6, 3};
a = cell2table(a);
a.a1 = categorical(string(a.a1))
a = 8×3 table
a1 a2 a3 ____________ __ __ marathon 1 4 bank holiday 3 6 bank holiday 2 1 concert 0 4 regatta 1 9 regatta 4 5 regatta 1 0 local fair 6 3
% Non-compact output (and plot)
s = scatter(a,'a2','a3','filled','ColorVariable','a1');
g = categories(a.a1);
h = colorbar;
h.YTick = [1:length(g)]; h.YTickLabel = g;
  1 件のコメント
Sim
Sim 2022 年 10 月 12 日
thanks a lot @Chunru!! I would accept both answers!!!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by