Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9250525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:27:49+00:00 2026-06-18T10:27:49+00:00

How can I plot a legend in a bar plot in Matlab? Here is

  • 0

How can I plot a legend in a bar plot in Matlab? Here is the code:

Y = [1.5056
0.72983
3.4530
3.2900
1.4839
12.9 ];
n = length(Y);
h = bar(Y);
colormap(summer(n));
grid on

l = cell(1,6);
l{1}='L'; l{2}='B'; l{3}='R'; l{4}='P'; l{5}='h'; l{6}='Ri';    
legend(h,l);

This give an error: Warning: Ignoring extra legend entries. I tried solutions which I found on the SO and web, but I couldn’t resolve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:27:50+00:00Added an answer on June 18, 2026 at 10:27 am

    Instead of legend, you can solve it using the tick labels for example:

    set(gca,'xticklabel', l) 
    

    enter image description here

    This will label each bar. If you want to use legend you need to have a matrix data, so the bar plot will show several bars per entry. For example

    Y=rand(10,6)
    h = bar(Y);
    colormap(summer(n));
    grid on
    l = cell(1,6);
    l{1}='L'; l{2}='B'; l{3}='R'; l{4}='P'; l{5}='h'; l{6}='Ri';    
    legend(h,l);
    

    enter image description here

    Or, you can use different bar() calls in this way:

    h = bar(diag(Y));
    

    But then you’ll get a displacement per each bar:

    enter image description here

    So, the only way really to do that using legend is to plot each bar seperatly, similar to what is discussed here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I move a ggplot2 legend to the bottom of the plot and
I m plotting one stacked bar plot where i need to write legend and
I'm using MATLAB to plot some data. I would like to create the legend
How can I improve the legend of spatial raster map plot using ggplot when
How I can remove the legend in a plot in R? I tried legend<-FALSE,
I wonder if there's any tool/website where I can plot some run times as
I know I can create a plot with line and dots using the type
Can I use the arrow() function in R to plot a transparent arrow between
How can I add a newline to a plot's label (e.g. xlabel or ylabel)
How can I calculate the maximum histogram value when making a plot? I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.