How to insert a unique legend with subplot? (2024)

51 views (last 30 days)

Show older comments

Armando MAROZZI on 3 May 2021

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/820170-how-to-insert-a-unique-legend-with-subplot

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/820170-how-to-insert-a-unique-legend-with-subplot

Commented: Armando MAROZZI on 3 May 2021

Accepted Answer: DGM

Open in MATLAB Online

I have the following plot:

y1 = [2,2,2,2];

y2 = [2.15, 2.115, 2.08, 2.07];

y3 = [0.08532, 0.071189, 0.05391, 0.06123];

y11 = [2,2,2,2];

y22 = [2.19,2.13,2.09,2.07];

y33= [0.1134, 0.1233,0.1122,0.1202];

y111 = [2,2,2,2];

y222 = [2.19,2.15,2.09,2.1];

y333= [0.1834, 0.1433,0.1622,0.1802];

x = [1:4];

h = figure;

subplot(1,3,1)

plot(x,y1,'*-r', 'LineWidth', 1)

hold on

plot(x, y2, '*-k', 'LineWidth',1)

hold on

plot(x, y3, '*-b', 'LineWidth',1)

hold on

plot(4.5, 2.08,'r.', 'MarkerSize', 20)

hold on

plot(4.5, 2.03,'k.', 'MarkerSize', 20)

hold on

plot(4.5, 0.0146331392,'b.', 'MarkerSize', 20)

box on

hold on

xticklabels({'Jan 7','Jan 14','Jan 21','Jan 28'})

xtickangle(45)

%ylim([-0.05 0.15])

title('January 2005')

set(gca,'Units','normalized')

titleHandle = get( gca ,'Title' );

pos = get(titleHandle , 'position' );

pos1 = pos + [0 0.005 0]

set(titleHandle , 'position' , pos1);

legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual','Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2)

subplot(1,3,2)

plot(x,y11,'*-r', 'LineWidth', 1)

hold on

plot(x, y22, '*-k', 'LineWidth',1)

hold on

plot(x, y33, '*-b', 'LineWidth',1)

hold on

plot(4.5,2.08,'r.', 'MarkerSize', 20)

hold on

plot(4.5,2.05,'k.', 'MarkerSize', 20)

hold on

plot(4.5,0.0938280876,'b.', 'MarkerSize', 20)

box on

hold on

xticklabels({'April 7','April 14','April 21','April 28'})

xtickangle(45)

title('April 2005')

set(gca,'Units','normalized')

titleHandle = get( gca ,'Title' );

pos = get(titleHandle , 'position' );

pos1 = pos + [0 0.005 0]

set(titleHandle , 'position' , pos1);

legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual', 'Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2)

subplot(1,3,3)

plot(x,y111,'*-r', 'LineWidth', 1)

hold on

plot(x, y222, '*-k', 'LineWidth',1)

hold on

plot(x, y333, '*-b', 'LineWidth',1)

hold on

line1 = plot(4.5,2.08,'r.', 'MarkerSize', 20)

hold on

line2 =plot(4.5,1.98,'k.', 'MarkerSize', 20)

hold on

line3 =plot(4.5,0.2170966370,'b.', 'MarkerSize', 20)

box on

hold on

xticklabels({'Aug 7','Aug 14','Aug 21','Aug 28'})

xtickangle(45)

title('August 2005')

set(gca,'Units','normalized')

titleHandle = get( gca ,'Title' );

pos = get(titleHandle , 'position' );

pos1 = pos + [0 0.005 0]

set(titleHandle , 'position' , pos1);

legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual', 'Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2)

In the plot, I have 3 legends for each plot which are identical. I would like to have one unique legend below and outside (centred) the plots. I have read suggestions around but I couldn't make it work.

Can anyone help me with this?

Thanks!

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

DGM on 3 May 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/820170-how-to-insert-a-unique-legend-with-subplot#answer_690510

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/820170-how-to-insert-a-unique-legend-with-subplot#answer_690510

Open in MATLAB Online

This is kind of roundabout, but maybe it helps:

subplot(1,3,1)

plot(x,y1,'*-r', 'LineWidth', 1)

hold on

plot(x, y2, '*-k', 'LineWidth',1)

hold on

plot(x, y3, '*-b', 'LineWidth',1)

hold on

plot(4.5, 2.08,'r.', 'MarkerSize', 20)

hold on

plot(4.5, 2.03,'k.', 'MarkerSize', 20)

hold on

plot(4.5, 0.0146331392,'b.', 'MarkerSize', 20)

box on

hold on

xticklabels({'Jan 7','Jan 14','Jan 21','Jan 28'})

xtickangle(45)

%ylim([-0.05 0.15])

title('January 2005')

set(gca,'Units','normalized')

titleHandle = get( gca ,'Title' );

pos = get(titleHandle , 'position' );

pos1 = pos + [0 0.005 0]

set(titleHandle , 'position' , pos1);

% when the legend is placed, it automatically resizes gca to a size we want

lh=legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual','Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2);

% store this size so we can use it later so that all axes leave room

axpos1 = get(gca,'outerposition');

% moving the legend changes the size of gca again

lh.Position(1) = 0.5-lh.Position(3)/2;

set(gca,'outerposition',axpos1) % so change it back

subplot(1,3,2)

plot(x,y11,'*-r', 'LineWidth', 1)

hold on

plot(x, y22, '*-k', 'LineWidth',1)

hold on

plot(x, y33, '*-b', 'LineWidth',1)

hold on

plot(4.5,2.08,'r.', 'MarkerSize', 20)

hold on

plot(4.5,2.05,'k.', 'MarkerSize', 20)

hold on

plot(4.5,0.0938280876,'b.', 'MarkerSize', 20)

box on

hold on

xticklabels({'April 7','April 14','April 21','April 28'})

xtickangle(45)

title('April 2005')

set(gca,'Units','normalized')

titleHandle = get( gca ,'Title' );

pos = get(titleHandle , 'position' );

pos1 = pos + [0 0.005 0]

set(titleHandle , 'position' , pos1);

% change this axes to make room for legend

axpos2 = get(gca,'outerposition');

set(gca,'outerposition',[axpos2(1) axpos1(2) axpos2(3) axpos1(4)])

subplot(1,3,3)

plot(x,y111,'*-r', 'LineWidth', 1)

hold on

plot(x, y222, '*-k', 'LineWidth',1)

hold on

plot(x, y333, '*-b', 'LineWidth',1)

hold on

line1 = plot(4.5,2.08,'r.', 'MarkerSize', 20)

hold on

line2 =plot(4.5,1.98,'k.', 'MarkerSize', 20)

hold on

line3 =plot(4.5,0.2170966370,'b.', 'MarkerSize', 20)

box on

hold on

xticklabels({'Aug 7','Aug 14','Aug 21','Aug 28'})

xtickangle(45)

title('August 2005')

set(gca,'Units','normalized')

titleHandle = get( gca ,'Title' );

pos = get(titleHandle , 'position' );

pos1 = pos + [0 0.005 0]

set(titleHandle , 'position' , pos1);

% change this axes to make room for legend

axpos3 = get(gca,'outerposition');

set(gca,'outerposition',[axpos3(1) axpos1(2) axpos3(3) axpos1(4)])

How to insert a unique legend with subplot? (3)

1 Comment

Show -1 older commentsHide -1 older comments

Armando MAROZZI on 3 May 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/820170-how-to-insert-a-unique-legend-with-subplot#comment_1497510

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/820170-how-to-insert-a-unique-legend-with-subplot#comment_1497510

quite helpful indeed! Thanks a lot!

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

  • plot
  • subplot
  • legend

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.


How to insert a unique legend with subplot? (5)

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

How to insert a unique legend with subplot? (2024)
Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6178

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.