4

I'm using an ipython notebook (python 2) and am plotting both a barchart and a line plot on the same plot. There are two series (NPS and Count Ratings). However, when I try to display the legend, it only shows a legend for the second series.

Below is my code:

ax=nps_funding_month[35:][nps_funding_month['count_ratings']>=100].set_index('funding_month')['nps_percentage'].\
plot(kind='line',color='green',label='NPS')

plt.ylabel('Net Promoter Score')

ax=nps_funding_month[35:][nps_funding_month['count_ratings']>=100].set_index('funding_month')['count_ratings'].\
plot(kind='bar',secondary_y=True,label='Count of Ratings')

plt.ylabel('Count Ratings')

plt.legend()

plt.title('Net Promoter Score by Funding Month\n(Only Funding Months with at Least 100 Reviews)')

Picture of Output

1

1 Answer 1

2

The following code

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df=pd.DataFrame({"x" : np.arange(5),
                 "a" : np.exp(np.linspace(3,5,5)),
                 "b" : np.exp(-np.linspace(-1,0.5,5)**2)})

ax=df.plot(x="x", y="a", kind='line',color='green',label='NPS')

plt.ylabel('Net Promoter Score')

ax2 = df.plot(x="x", y="b", kind='bar',secondary_y=True,label='Count of Ratings', ax=ax)

plt.ylabel('Count Ratings')

plt.title('Superlongtitle that is not needed')  
plt.show()

produces

enter image description here

Note that the first axes is given as argument to the second pandas plot (ax=ax) and no legend is added via pyplot (it comes automatically through pandas).

The problem may then be that the legend is hidden by the bars. The reason for that is that the legend resides in the first (lower) axes. There are two options.

  1. We could move it to the secondary axes and then also change its location.

    leg = ax.get_legend()
    leg.remove()        # remove it from ax
    ax2.add_artist(leg) # add it to ax2
    leg._set_loc(4)
    

    Where the the loc 4 means "lower left" and is one of the codes to place the legend.

  2. We can move it out of the plot, (as in How to put the legend out of the plot)

    leg._set_loc(2)
    leg.set_bbox_to_anchor((1.1,1))
    ax.figure.subplots_adjust(right=0.6) # make space for the legend outside
    
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome. This works to an extent (the legend overlaps with one of the bars). Could you add to this how you would move the legend somewhere else? Much appreciated!
Also note that the method presented in this answer only works when the same dataframe is used for both plots. In case two different dataframes are used, refer to the answers from this question.
Wow thanks. I've been searching SO for days to figure this out.
As of pandas 1.3.3 this example no longer works. You should replace label with legend.

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.