How to simplify legend when grouping by 2 variables, with unique colors for one and unique shapes for the other
3 ビュー (過去 30 日間)
古いコメントを表示
This question is a follow-up to my previously-answered question: https://www.mathworks.com/matlabcentral/answers/797552-how-to-group-by-2-variables-with-unique-colors-for-one-and-unique-shapes-for-the-other?s_tid=prof_contriblnk
The offered solution provides a legend with every combination of the two variables being grouped by. How do I simplify it, so that Input is shown once for each associated color (maybe all points for the shape), and each Year shape is shown once, in black?
Here is the code from the solution:
% Original data
Year = [2016 2016 2017 2017 2018 2018 2016 2016 2017 2017 2018 2018 2016 2016 2017 2017 2018 2018];
Input = [0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2];
Response = [4704.5 4059.5 10891 11440.5 4083.5 2876 11459.66667 11752 11566 12036 11323.5 11118.5 10296.5 10234 13074.5 14166 9062 9669];
% Reormat the data
Year = min(reshape(Year,2,[]),[],1)';
Input = min(reshape(Input,2,[]),[],1)';
Response = reshape(Response,2,[])';
% Visualize the data
Data = array2table([Year,Input,Response],'VariableNames',["Year","Input","T1","T2"])
figure
mrkr = {'.','s','^'};
ax = axes;
hold on
Yr = unique(Data.Year);
for y = 1:length(Yr)
ind = Data.Year == Yr(y);
% just plot data for this year, specifying marker style and turning legend off
gscatter(ax,Data.T1(ind),Data.T2(ind),Data.Input(ind),[],mrkr{y},[],'off');
ax.ColorOrderIndex = 1;
end
hold off
% Use some string manipulation to create legend labels for all color+marker combinations
legend(Yr' + "_" + unique(Data.Input),'NumColumns',length(Yr),'Interpreter',"none",'Location',"best")
0 件のコメント
回答 (1 件)
Vatsal
2023 年 12 月 27 日
Hi,
I understand you want to simplify the legend so that each "Input" is associated with a unique color, and each "Year" shape is represented once in black.
Below is the modified code for that purpose:
Year = [2016 2016 2017 2017 2018 2018 2016 2016 2017 2017 2018 2018 2016 2016 2017 2017 2018 2018];
Input = [0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2];
Response = [4704.5 4059.5 10891 11440.5 4083.5 2876 11459.66667 11752 11566 12036 11323.5 11118.5 10296.5 10234 13074.5 14166 9062 9669];
Year = min(reshape(Year,2,[]),[],1)';
Input = min(reshape(Input,2,[]),[],1)';
Response = reshape(Response,2,[])';
Data = array2table([Year,Input,Response],'VariableNames',["Year","Input","T1","T2"])
figure
mrkr = {'.','s','^'};
ax = axes;
hold on
Yr = unique(Data.Year);
h = []; % Initialize an empty array to hold your plot handles
for y = 1:length(Yr)
ind = Data.Year == Yr(y);
% Store the plot handles when plotting
h = [h; gscatter(ax,Data.T1(ind),Data.T2(ind),Data.Input(ind),[],mrkr{y},[],'off')];
ax.ColorOrderIndex = 1;
end
hold off
% Create a legend for the colors (Inputs)
legend(h(1:3), 'Input 0', 'Input 1', 'Input 2', 'Location', 'northwest')
% Create a second axes for the second legend
ax2 = axes('Position',ax.Position,'Visible','off');
% Create a legend for the shapes (Years)
line('Parent',ax2,'XData',nan,'YData',nan,'Marker',mrkr{1},'Color','k','DisplayName','Year 2016', 'MarkerFaceColor', 'k', 'LineStyle', 'none');
line('Parent',ax2,'XData',nan,'YData',nan,'Marker',mrkr{2},'Color','k','DisplayName','Year 2017', 'MarkerFaceColor', 'k', 'LineStyle', 'none');
line('Parent',ax2,'XData',nan,'YData',nan,'Marker',mrkr{3},'Color','k','DisplayName','Year 2018', 'MarkerFaceColor', 'k', 'LineStyle', 'none');
legend(ax2,'Location','southeast') % Change this to 'southeast' for bottom right
I hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!