在python中有Matplotlib的辅助子图时,stackplot图例问题

时间:2016-09-29 21:11:58

标签: python matplotlib subplot

这是我的代码,下面是产生的错误。

我的传奇出现了,但其中只有一条线 堆叠的子图标签不会出现。

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 <matplotlib.collections.PolyCollection    object at 0x10b292590>
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

1 个答案:

答案 0 :(得分:0)

所以我考虑了一下并搜索了一些解决方案。以下是我使用的代码行。

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