stackplot legend issue when there is a secondary subplot for Matplotlib in python
<p>Here is my code and below it is the error produced. </p>

<p>My legend appears, but only one of the lines is in it.<br>
The stacked subplot labels do not appear.  </p>

<pre><code>import xlrd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

file_location = "/Users/adampatel/Desktop/psw02.xls"
workbook = xlrd.open_workbook(file_location)
worksheet = workbook.sheet_by_name('Data 1')

x = [worksheet.cell_value(i+1425, 0) for i in range(worksheet.nrows-1425)]
y1 = [worksheet.cell_value(i+1425, 1) for i in range(worksheet.nrows-1425)]
y2 = [worksheet.cell_value(i+1425, 25) for i in range(worksheet.nrows-1425)]
y3 = [worksheet.cell_value(i+1425, 35) for i in range(worksheet.nrows-1425)]
y4 = [worksheet.cell_value(i+1425, 41) for i in range(worksheet.nrows-1425)]
y5 = [worksheet.cell_value(i+1425, 50) for i in range(worksheet.nrows-1425)]


fig = plt.figure()
ax = fig.add_subplot()

start_date = datetime.date(1899, 12, 30)
dates=[start_date + datetime.timedelta(xval) for xval in x]
ax.xaxis.set_major_locator(mdates.MonthLocator((), bymonthday=1, interval=6))
ax.xaxis.set_minor_locator(mdates.MonthLocator((), bymonthday=1, interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b'%y"))

ly1 = ax.plot(dates, y1, '-k', label = 'Oil Intake (LHS)')
ax2 = ax.twinx()
ly2 = ax2.stackplot(dates, y2, y3, y4, y5, colors=['0.2','0.4','0.6','0.8'], label=['gasoline', 'kerosene', 'distillates', 'residuals'])

ly1y2 = ly1+ly2
labs = [l.get_label() for l in ly1y2]
ax.legend(ly1y2, labs, fontsize = 10, loc = 2)

ax.set_ylim(11500,17500)
ax2.set_ylim(8000, 28000)
plt.show()

=================== RESTART: /Users/adampatel/Desktop/1.py ===================

Warning (from warnings module):
  File     "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/legend.py", line 613
(str(orig_handle),))
UserWarning: Legend does not support &lt;matplotlib.collections.PolyCollection    object at 0x10b292590&gt;
Use proxy artist instead.
</code></pre>

<p><a href="http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist" rel="nofollow">http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist</a></p>

<p>So I thought about it and googled a few solutions.  Here are the lines of code that I used.  </p>

<pre><code>import xlrd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.patches as mpatches
import datetime

file_location = "/Users/adampatel/Desktop/psw02.xls"
workbook = xlrd.open_workbook(file_location, on_demand = False)
worksheet = workbook.sheet_by_name('Data 1')

x = [worksheet.cell_value(i+1425, 0) for i in range(worksheet.nrows-1425)]
y1 = [worksheet.cell_value(i+1425, 1) for i in range(worksheet.nrows-1425)]
y2 = [worksheet.cell_value(i+1425, 25) for i in range(worksheet.nrows-1425)]
y3 = [worksheet.cell_value(i+1425, 35) for i in range(worksheet.nrows-1425)]
y4 = [worksheet.cell_value(i+1425, 41) for i in range(worksheet.nrows-1425)]
y5 = [worksheet.cell_value(i+1425, 50) for i in range(worksheet.nrows-1425)]


fig = plt.figure(figsize = (10, 7))
ax = fig.add_subplot(111)

start_date = datetime.date(1899, 12, 30)
dates=[start_date + datetime.timedelta(xval) for xval in x]
ax.xaxis.set_major_locator(mdates.MonthLocator((), bymonthday=1, interval=6))
ax.xaxis.set_minor_locator(mdates.MonthLocator((), bymonthday=1, interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b'%y"))

ly1, = ax.plot(dates, y1, '-k', linewidth=2.0, label = 'Oil Intake (LHS)')
ax2 = ax.twinx()
ly2 = ax2.stackplot(dates, y2, y3, y4, y5, colors=['0.2','0.4','0.6','0.8'], label=['gasoline', 'kerosene', 'distillates', 'residuals'])


ax.grid()
ax.set_ylabel("Thousands of Barrels per Day")
ax2.set_ylabel("Thousands of Barrels per Day")
ax.set_ylim(11500,17500)
ax2.set_ylim(8000, 28000)
plt.legend([ly1, mpatches.Patch(color='.2'), mpatches.Patch(color='.4'), mpatches.Patch(color='.6'), mpatches.Patch(color='.8')], ['Oil Intake (LHS)', 'Gasoline','Kerosene','Distillates', 'Residuals'], loc = 2)
plt.title('Refinar-Blendar Intake')
plt.savefig('Refinar Blendar2.png', bbox_inches='tight', dpi=90)
plt.show()
</code></pre>

python matplotlib subplot