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 8189239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:12:38+00:00 2026-06-07T03:12:38+00:00

I have a pandas DataFrame and I want to plot a bar chart that

  • 0

I have a pandas DataFrame and I want to plot a bar chart that includes a legend.

import pylab as pl
from pandas import *

x = DataFrame({"Alpha": Series({1: 1, 2: 3, 3:2.5}), "Beta": Series({1: 2, 2: 2, 3:3.5})})

If I call plot directly, then it puts the legend above the plot:

x.plot(kind="bar")

If I turn of the legend in the plot and try to add it later, then it doesn’t retain the colors associated with the two columns in the DataFrame (see below):

x.plot(kind="bar", legend=False)
l = pl.legend(('Alpha','Beta'), loc='best')

What’s the right way to include a legend in a matplotlib plot from a Pandas DataFrame?
enter image description here

  • 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-07T03:12:40+00:00Added an answer on June 7, 2026 at 3:12 am

    If you want to add the legend manually, you have to ask the subplot for the elements of the bar plot:

    In [17]: ax = x.plot(kind='bar', legend=False)
    
    In [18]: patches, labels = ax.get_legend_handles_labels()
    
    In [19]: ax.legend(patches, labels, loc='best')
    Out[19]: <matplotlib.legend.Legend at 0x10b292ad0>
    

    Also, plt.legend(loc='best') or ax.legend(loc='best') should “just work”, because there are already “links” to the bar plot patches set up when the plot is made, so you don’t have to pass a list of axis labels.

    I’m not sure if the version of pandas you’re using returns a handle to the subplot (ax = ...) but I’m fairly certain that 0.7.3 does. You can always get a reference to it with plt.gca().

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

Sidebar

Related Questions

I have a pandas DataFrame that has multiple columns in it: Index: 239897 entries,
I have a pandas DataFrame with a date column. It is not an index.
I have the data below saved as a pandas dataframe . With this data,
I have a pandas panel of investment pricing data to which I want to
I have the following Pandas Dataframe with a MultiIndex(Z,A): H1 H2 Z A 0
On Pandas documentation of the pivot method, we have: Examples -------- >>> df foo
I have a list of csv files ( file1, file2, ... ) that have
Have converted devise new session from erb to Haml but doens't work, this is
I have create a class that extends BaseAdapter . At the same time, tt
Have finally got a responsive site working (of a fashion). What I want to

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.