gplotmatrix compatability with tiledlayout

I am trying to combine tiledlayout with gplotmatrix because of the ease of adding legends directly above multiple plots. Using a simple plot I can and commenting out the gplotmatrix line, I can get the legend that is directly north of the plot. However there appears to be compatibility issues with gplotmatrix as I get a warning "Property assignment is not allowed when the object is empty. Use subscripted assignment to create an array element."
Ideally, gplotmatrix would allow the legend to behave like tiledlayouts with ease of adding xlabels, ylabels, titles, and legends. However I have found that getting a center legend above the gplotmatrix is rather difficult.
randMat = randn(25, 5);
VarNames = "Var " + string((1:5))';
catNames = [repmat('Class A', 20, 1); repmat('Class B', 5, 1)];
catTable = table(catNames, 'VariableNames', {'Class Type'});
MyDataTable = array2table(randMat, "VariableNames", VarNames);
MyDataTable = [MyDataTable, catTable];
tt = tiledlayout(1,1);
nexttile
% plot(randi(100, 10, 1)) Works with legend due north
gplotmatrix(MyDataTable{:, 1:5}, [], MyDataTable.("Class Type"), [],[], [], true, 'stairs', VarNames)
lgd = legend();
lgd.Layout.Tile = 'north';

답변 (1개)

Madheswaran
Madheswaran 2024년 9월 9일

1 개 추천

Hi Adam,
The issue you are encountering is because figure created using the ‘tiledlayout’ gets overwritten when you plot using ‘gplotmatrix’. You can verify this by printing the children of the current figure before and after ‘gplotmatrix’ function is used.
disp(gcf().Children) % prints 'TiledChartLayout'
gplotmatrix(MyDataTable{:, 1:5}, [], MyDataTable.("Class Type"), [],[], [], true, 'stairs', VarNames)
disp(gcf().Children) % prints '32 * 1 graphics array'
If your objective is to show the legend outside the plots of ‘gplotmatrix’, you can directly modify the position property of the legend, like outlined in the below code:
%... existing code to create MyDataTable
MyDataTable = [MyDataTable, catTable];
fig = figure;
gp = gplotmatrix(MyDataTable{:, 1:5}, [], MyDataTable.("Class Type"), [],[], [], true, 'stairs', VarNames);
% Find the legend handle
legHandle = findobj(fig, 'Type', 'Legend');
% Move the legend outside the plot
if ~isempty(legHandle)
legHandle.Position = [0.5,0.95, 0.035, 0.025];
end
Above code will place the legend above plots created by ‘gplotmatrix’ as shown below:
Refer to the MathWorks documentations for more information on the following topics:
  1. Legend Properties - https://mathworks.com/help/matlab/ref/matlab.graphics.illustration.legend-properties.html
  2. gplotmatrix - https://mathworks.com/help/stats/gplotmatrix.html
Hope this helps!

댓글 수: 1

Jan Kappen
Jan Kappen 2024년 9월 23일
This is imo just a workaround. Why is gplotmatrix (and plotmatrix) not supporting tiledlayouts is the key question.

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2021b

질문:

2022년 3월 11일

댓글:

2024년 9월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by