32

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

0

2 Answers 2

64

The most succinct way to go is:

x.plot(kind="bar").legend(bbox_to_anchor=(1.2, 0.5))

or in general

x.plot(kind="bar").legend(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

2 Comments

This is the best answer.
@AliHashemi The reason this is not the accepted answer is because it does not answer the original question, which was also how to set specific colors. This answer does not show how to do that. Incidentally, Wes McKinney is the main author of pandas.
31

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.