1

I'm having trouble with the legends in a simple pandas plotting task. It seems the second legend is being hidden by the first graph.

A sample of my df:

print(campaign.head(10))

    hour_read  hour_sent
0        16.0         13
1        14.0         13
2        13.0         13
3        14.0         13
4         NaN         13
5        13.0         13
6        13.0         13
7        16.0         13
8        14.0         13
9        16.0         13
10       15.0         15
11       16.0         15
12       15.0         15
13       15.0         15
14       15.0         15

I can plot both these series on the same histogram, and I am using `secondary_y'.

However I can't get the legend for hour_read to appear. No matter what I try with the legend options.

plt.figure(figsize=(15,8))
plt.xlabel('Email sent vs open hour')

campaign.hour_read.plot(kind='hist',alpha=0.8,  label='Hour Read')

campaign.hour_sent.plot(kind='hist',alpha=0.8 ,secondary_y=True, label='Hour Sent')

plt.legend(loc='upper right')


plt.show()

enter image description here

I have also tried setting up separate axes first. And using #ax1.legend(loc=1) and ax2.legend(loc=2) with many different variations. I think the hour sent plot is completely covering the hour_read legend. How can I get around this?

Community
  • 1
  • 1
SCool
  • 3,104
  • 4
  • 21
  • 49
  • Does this answer your question:https://stackoverflow.com/questions/45777023/pandas-plot-multiple-series-but-only-showing-legend-for-one-series? – Pygirl Feb 28 '20 at 10:15
  • 1
    if I am not working campaign.hour_read and campaign.hour_sent both are different axes, can you try this campaign.hour_read.legend(loc='upper right') campaign.hour_sent.legend(loc='upper right') – The Guy Feb 28 '20 at 10:17
  • https://stackoverflow.com/questions/21988196/legend-only-shows-one-label-when-plotting-with-pandas – Pygirl Feb 28 '20 at 10:18
  • @Pygirl this removes legends completely. – SCool Feb 28 '20 at 10:20
  • @Pygirl the second recommended link gives me the error: `'AxesSubplot' object has no attribute 'right_ax'` – SCool Feb 28 '20 at 10:22
  • @TheGuy a variation of your comment worked. You had them both set at `upper right`. I changed one to `upper left`. You were also assigning a legend to the pandas series itself. This is what I did: `campaign.hour_read.plot(kind='hist',bins=24,alpha=0.8, label='Hour Read').legend(loc='upper right')` – SCool Feb 28 '20 at 10:24
  • so is it working? – The Guy Feb 28 '20 at 10:26
  • 1
    @TheGuy yep, thanks – SCool Feb 28 '20 at 10:29

1 Answers1

1
fig=plt.figure()

plt.xlabel('Email sent vs open hour')

campaign.hour_read.plot(kind='hist',alpha=0.8,  label='Hour Read')

campaign.hour_sent.plot(kind='hist',alpha=0.8 ,secondary_y=True, label='Hour Sent')


handles,labels = [],[]
for ax in fig.axes:
    for h,l in zip(*ax.get_legend_handles_labels()):
        handles.append(h)
        labels.append(l)

plt.legend(handles,labels)
plt.show()

enter image description here

Pygirl
  • 12,969
  • 5
  • 30
  • 43