Merging different legend labels (2024)

47 views (last 30 days)

Show older comments

Mahmut Cenk on 8 Jul 2024 at 10:05

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels

Commented: Mahmut Cenk about 22 hours ago

Accepted Answer: VINAYAK LUHA

  • untitled.jpg

Open in MATLAB Online

Hello everyone, I haven't spend much time coding in Matlab so this one might be a little silly question. Similar questions have been asked as far as I can see; but none of the solutions provided worked for me. So here I am asking: As can be seen in the attached code and image, I have a graph consisting of five lines, two of them representing the resulst of the first model (green ones) and the rest representing the second model (blue ones). Could not find a way to add legend label for different colours in editor. When I code and graph without legends and insert legend in the figure window, it happens to be that, as expected, there are five different labels for each line. I want to merge the green and blue ones and I am pretty sure it is possible and it is just me who cannot do it. Also in the future 8 more model results will be added, each of them consisting of one, two or three lines. How can I merge the legend labels?

Thanks in advance!

x1 = [1, 2 ,3, 4, 5];

y1 = [5, 3, 2.8, 1.7, 1.2];

y2 = [6.5, 4.7, 3.5, 1.9, 1.3];

x2 = [1, 2, 3, 4, 5];

y3 = [6, 5.6, 3.1, 2.9, 1.7];

y4 = [5, 4.1, 3.8, 2, 1.5];

y5 = [5, 4, 3, 1.8, 1.4];

plot(x1,y1,x1,y2,'color', 'g'); %Model 1

hold on

plot(x2,y3,x2,y4,x2,y5,'color', 'b'); %Model 2

hold off

grid minor

xlabel('x values')

ylabel('y values')

title('figure 1')

Merging different legend labels (2)

1 Comment

Show -1 older commentsHide -1 older comments

Mahmut Cenk about 22 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#comment_3205716

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#comment_3205716

Endless thanks to everyone answered. All three suggestions work really well. Thank you guys.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

VINAYAK LUHA about 23 hours ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#answer_1482746

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#answer_1482746

Open in MATLAB Online

Hi Mahmut,

My understanding is that you have multiple line plots from different models and want to create a common legend for all line plots from a single model.

You can achieve this by using the "graphics object handles" of the plots along with the MATLAB "legend" function.

Here is a code snippet for your reference:

x1 = [1, 2 ,3, 4, 5];

y1 = [5, 3, 2.8, 1.7, 1.2];

y2 = [6.5, 4.7, 3.5, 1.9, 1.3];

x2 = [1, 2, 3, 4, 5];

y3 = [6, 5.6, 3.1, 2.9, 1.7];

y4 = [5, 4.1, 3.8, 2, 1.5];

y5 = [5, 4, 3, 1.8, 1.4];

hold on;

% Plot Model 1

h1 = plot(x1, y1, 'color', 'g');

plot(x1, y2, 'color', 'g');

% Plot Model 2

h2 = plot(x2, y3, 'color', 'b');

plot(x2, y4, 'color', 'b');

plot(x2, y5, 'color', 'b');

% Add grid, labels, and title

grid minor;

xlabel('x values');

ylabel('y values');

title('Figure 1');

% Add legend

legend([h1, h2], {'Model 1', 'Model 2'});

hold off;

Merging different legend labels (5)

For more details on the MATLAB "legend" and "Graphics Object handle", you can refer to the following documentations:

  • https://www.mathworks.com/help/matlab/ref/legend.html
  • https://www.mathworks.com/help/matlab/creating_plots/how-to-work-with-graphics-objects.html

Hope this answers your question

Regards,

Vinayak

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (2)

Matt J about 24 hours ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#answer_1482716

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#answer_1482716

Edited: Matt J about 24 hours ago

Open in MATLAB Online

One way,

x1 = [1, 2 ,3, 4, 5];

y1 = [5, 3, 2.8, 1.7, 1.2];

y2 = [6.5, 4.7, 3.5, 1.9, 1.3];

x2 = [1, 2, 3, 4, 5];

y3 = [6, 5.6, 3.1, 2.9, 1.7];

y4 = [5, 4.1, 3.8, 2, 1.5];

y5 = [5, 4, 3, 1.8, 1.4];

Model1=plot(x1,y1,x1,y2,'color', 'g'); %Model 1

hold on

Model2=plot(x2,y3,x2,y4,x2,y5,'color', 'b'); %Model 2

hold off

grid minor

xlabel('x values')

ylabel('y values')

title('figure 1')

legend([Model1(1), Model2(1)],'Model 1','Model 2')

Merging different legend labels (7)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Catalytic about 24 hours ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#answer_1482731

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2135451-merging-different-legend-labels#answer_1482731

Open in MATLAB Online

x1 = [1, 2 ,3, 4, 5];

y1 = [5, 3, 2.8, 1.7, 1.2];

y2 = [6.5, 4.7, 3.5, 1.9, 1.3];

x2 = [1, 2, 3, 4, 5];

y3 = [6, 5.6, 3.1, 2.9, 1.7];

y4 = [5, 4.1, 3.8, 2, 1.5];

y5 = [5, 4, 3, 1.8, 1.4];

X1=[x1 nan x1]; Y1=[y1 nan y2];

X2=[x2 nan x2 nan x2]; Y2=[y3 nan y4 nan y5];

plot(X1,Y1,'g', X2,Y2,'b'); legend 'Model 1' 'Model 2'

grid minor

xlabel('x values')

ylabel('y values')

title('figure 1')

Merging different legend labels (9)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

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

  • plots
  • legend
  • legend labels

Products

  • MATLAB

Release

R2022b

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.


Merging different legend labels (10)

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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Merging different legend labels (2024)
Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6634

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.