Legend in TiledLayout with AppDesigner

3 views (last 30 days)
Marius Kaffka
Marius Kaffka on 28 Apr 2022
Answered: Binaya on 18 Oct 2023
I have a function that plots a tiledLayout into a figure.
fig = figure('Name',fileU,...
'NumberTitle', 'off',...
'Units', "centimeters",...
'Position', [3 3 app.breite app.hoehe]);
erstelleDiagramm(app,dateiName(1),'ISO') % This is the function to fill fig
The function extracts data to put in the plot.
I plot my data in a 2x4 tile and create a legend for this plot.
ISOPlot = tiledlayout(3,6,...
TileSpacing="compact",...
Padding = "compact",...
Units='centimeters');
nexttile(1,[2 4])
hold on
grid on
box on
%% Some of my plotting (K is a struct storing my data)
pdpy_up = plot(K.P, K.x_up,'o',...
'MarkerEdgeColor',[ 0, 84, 159]/255,...
'MarkerFaceColor',[ 0, 84, 159]/255,...
'MarkerSize',6);
%% Adding a table
uit_left = uitable(...
'Position',[0 10 407 130],...
"Units","centimeters",...
"FontSize",8);
uit_left.ColumnName = {'ZTZ - Analysis features', '(+) µm', '(-) µm', '(Bidir) µm'};
uit_left.Data = {...
'',999,999,999;...
'',999,999,999;...
'',999,999,999;...
'',999,999,999;...
'',999,999,999;...
'',999,999,999;...
};
I want to place the legend in the upper right corner next to the plot.
%% LEGENDE
lgd = legend([pdpy_up(1) pdpy_down(1) px_mean_up(1) px_mean_down(1) px_mean_bi(1) b g],...
'Errors (+)',... %Abweichung
'Errors (-)',... %Fehler
'Mean (+)',... %Durchschnitt
'Mean (-)',... %Durchschnitt
'Mean deviations',... %Mittlere Abweichung
'Repeatability (+)',... %Wiederholung
'Repeatability (-)'); %Wiederholung
ylabel("Error / µm",...
"FontSize",10) %Fehler
xlabel('Axis position / mm',...
"FontSize",10) %Achsenposition
%lgd.Layout.Tile = 5;
% lgd.Units = 'centimeters';
% lgd.Position = [22.3,9.4,4,4];
lgd.Units = 'normalized';
lgd.Position = [0,0,.5,.5];
Now playing with lgd.layout.Tile and .Units/.Position creates crazy different results.
I would like to place it a the coordinates given in lgd.Position.
But using the uncommented lgd.Units/.Position creates a huge legend.
To finish up, I want to save my figure
file = "Test.emf";
saveas(ISOPlot,file,'meta')
And sometimes saving manually in the figure window compared to the saveas func creates different results as well.

Answers (1)

Binaya
Binaya on 18 Oct 2023
Hi Marius,
Based on my understanding, you would like to generate a legend in the top-right corner of the plot.
Upon reviewing the provided code, it is evident that the legend's “Units” and “Position” are set to "normalized" and "[0 0 0.5 0.5]" respectively. This implies that the position and size of the legend tile are specified in normalized units, where the entire plot dimensions are represented as 1 in both the x and y directions. The first two elements of the position array indicate the position of the bottom-left corner of the legend tile, while the next two elements denote the width and height of the legend tile. By setting the height and width to 0.5, the size of the legend tile becomes 0.5 times that of the entire plot, resulting in an excessively large legend tile.
One example of keeping the legend at top right corner using “Position” property is given below:
lgd.Position = [0.8 0.8 0.1 0.1];
Please refer to below Mathworks Documentation for more details:
  1. Legend Properties: https://www.mathworks.com/help/matlab/ref/matlab.graphics.illustration.legend-properties.html#:~:text=Position%20%E2%80%94%20Custom%20location%20and%20size
I hope this helps.
Regards
Binaya

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!