gplotmatrix compatability with tiledlayout

10 views (last 30 days)
Adam Watts
Adam Watts on 11 Mar 2022
Commented: Jan Kappen on 23 Sep 2024
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';

Answers (1)

Madheswaran
Madheswaran on 9 Sep 2024
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 Comment
Jan Kappen
Jan Kappen on 23 Sep 2024
This is imo just a workaround. Why is gplotmatrix (and plotmatrix) not supporting tiledlayouts is the key question.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!