Legend Placement in Nested Tile Layout (2024)

25 views (last 30 days)

Show older comments

Ilan Wolff on 27 Dec 2023

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2064531-legend-placement-in-nested-tile-layout

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2064531-legend-placement-in-nested-tile-layout

Moved: Dyuman Joshi on 28 Dec 2023

Accepted Answer: Harsha Vardhan

Open in MATLAB Online

Here is the figure my code currently creates:

Legend Placement in Nested Tile Layout (2)

I want to produce exaclty this same figure, but with the legend in the bottom middle, rather than the bottom. As I understand it, the problem is that legend takes in the axes for the second tile in the nested layout as its position argument. I think I need to use a nested tiled layout in order to get a title for each column (currently "test1" and test2").

My code is paasted below. I really appreciate any help/suggestions!

figure('Position', [0, 0, 1200, 600]); % Adjust the size as needed

T=tiledlayout(1, length(cdfs_in)); %

for i=1:length(cdfs_in)

t=tiledlayout(T,2,1);

title(t, titles(i));

t.Layout.Tile = i;

t.Layout.TileSpan = [1 1];

t.TileSpacing = 'compact';

nexttile(t);

plot(soph, utils_sinc(:,1,i))

hold on

plot(soph, utils_sinc(:,2,i))

hold on

plot(soph, utils_sinc(:,3,i))

hold off

ylabel("Mean Utility", 'FontSize', 12);

xlabel("Fraction of Students who are Sophisticated (P_{soph})", 'FontSize', 12);

title("Sincere Students", 'FontWeight', 'normal');

plot(soph, utils_tot(:,1,i))

hold on

plot(soph, utils_tot(:,2,i))

hold on

plot(soph, utils_tot(:,3,i))

hold off

title("All Students", 'FontWeight', 'normal');

xlabel("Fraction of Students who are Sophisticated (P_{soph})", 'FontSize', 12);

ylabel("Mean Utility", 'FontSize', 12);

end

lgd = legend('Optimal Taiwan Mechanism', 'Boston Mechanism', 'Deferred Acceptance', 'location', 'Best');

lgd.Layout.Tile = 'south';

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Harsha Vardhan on 28 Dec 2023

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2064531-legend-placement-in-nested-tile-layout#answer_1379436

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2064531-legend-placement-in-nested-tile-layout#answer_1379436

Open in MATLAB Online

Hi,

I understand that you want the legend to be displayed at the bottom middle rather than at the bottom.

Based on your description, the issue arises because the legend is currently aligned with the axes of the second tile in your nested layout. Let's address this to achieve the desired placement.

I recreated your figure using dummy data and viewed the figure in Property Inspector as below.

Legend Placement in Nested Tile Layout (4)

We can notice that the legend, by default, is linked as a child to the most recently active tiled layout. In your case, it's the inner layout, hence the legend aligning to the bottom of the second tile. Your current approach using

lgd.Layout.Tile = 'south';

is south-aligning the legend as a child to this inner layout created in the line ‘

t=tiledlayout(T,2,1);

To overcome this, we need to redirect the legend's association as a child from the inner tiled layout to the outer one. However, directly changing the parent of the legend using

lgd.Parent = T;

leads to an error as below due to the legend's linked axes belonging to the inner layout.

'A legend and its associated axes must have the same parent'

This is because the axes associated with the legend is the most recently created axes and is a child of the inner tiled layout. This is the axes created in the second execution of

The solution is to create an auxiliary, invisible axis associated with the outer layout. This axis will act as a placeholder for the legend without altering your current figure structure. Here's how we can do it:

  1. Generate a dummy axis in the outer layout.
  2. Plot invisible or NaN data on this axis, ensuring it doesn't disrupt your existing plot.
  3. Assign the legend to this dummy axis and position it at the bottom middle, achieving the layout you desire.

This approach maintains the structural integrity of your nested layout while allowing the flexibility to position the legend as needed.

Please check the modified code that implements this solution.

%Generating Dummy data for the purpose of this solution

% Assuming cdfs_in represents some groups or conditions

cdfs_in = 1:2; % For example, we have 2 conditions

% Define titles for the tiled plots

titles = {'test 1', 'test 2'};

% 'soph' can be a linear space vector, representing a fraction of sophisticated students

soph = linspace(0.3, 0.7, 100); % 100 points between 0.3 and 0.7

% 'utils_tot' is a 3D matrix for utility values with dimensions corresponding to:

% - number of 'soph' points (100)

% - number of strategies/mechanisms (3)

% - number of conditions in 'cdfs_in' (length of cdfs_in)

utils_sinc = rand(100, 3, length(cdfs_in)) * 0.5 + 1; % Random data for sincere students

utils_tot = rand(100, 3, length(cdfs_in)) * 0.5 + 1; % Random data for all students

figure('Position', [0, 0, 1200, 600]); % Adjust the size as needed

T=tiledlayout(1, length(cdfs_in));

for i=1:length(cdfs_in)

t=tiledlayout(T,2,1);

title(t, titles(i));

t.Layout.Tile = i;

t.Layout.TileSpan = [1 1];

t.TileSpacing = 'compact';

nexttile(t);

plot(soph, utils_sinc(:,1,i))

hold on

plot(soph, utils_sinc(:,2,i))

hold on

plot(soph, utils_sinc(:,3,i))

hold off

ylabel("Mean Utility", 'FontSize', 12);

xlabel("Fraction of Students who are Sophisticated (P_{soph})", 'FontSize', 12);

title("Sincere Students", 'FontWeight', 'normal');

nexttile(t)

plot(soph, utils_tot(:,1,i))

hold on

plot(soph, utils_tot(:,2,i))

hold on

plot(soph, utils_tot(:,3,i))

hold off

title("All Students", 'FontWeight', 'normal');

xlabel("Fraction of Students who are Sophisticated (P_{soph})", 'FontSize', 12);

ylabel("Mean Utility", 'FontSize', 12);

end

% Create a dummy axes for placing the legend at the bottom

ax = axes(T, 'Visible', 'off');

% Plot dummy lines with the same properties as your actual data plots

hold(ax, 'on'); % Hold the dummy axes for multiple plots

hOptimal = plot(ax, NaN, NaN, 'DisplayName', 'Optimal Taiwan Mechanism', 'Color', 'b');

hBoston = plot(ax, NaN, NaN, 'DisplayName', 'Boston Mechanism', 'Color', 'r');

hDeferred = plot(ax, NaN, NaN, 'DisplayName', 'Deferred Acceptance', 'Color', 'y');

hold(ax, 'off'); % Release the hold

% Create the legend using the dummy lines

lgd = legend([hOptimal, hBoston, hDeferred]);

lgd.Layout.Tile = 'south';

Please check the resulting figure below.

Legend Placement in Nested Tile Layout (5)

Hope this helps in resolving your query!

1 Comment

Show -1 older commentsHide -1 older comments

Ilan Wolff on 28 Dec 2023

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2064531-legend-placement-in-nested-tile-layout#comment_3011426

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2064531-legend-placement-in-nested-tile-layout#comment_3011426

Moved: Dyuman Joshi on 28 Dec 2023

This is beautiful. Thank you so much!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Find more on Legend in Help Center and File Exchange

Tags

  • tiledlayout
  • legend

Products

  • MATLAB Coder

Release

R2022a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Legend Placement in Nested Tile Layout (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Legend Placement in Nested Tile Layout (2024)
Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6295

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.