0

升级我们的 matplotlib 版本后,我们现在使用figlegend(). 它只显示一个空框,如下所示:

在此处输入图像描述

我见过这个这个,但到目前为止都没有帮助我。

我正在使用该功能

handles, labels = ax.get_legend_handles_labels()

这以前工作得很好。其他一切都按预期工作。

我已经包含了一段非常精简的代码摘录,我希望能说明我们如何处理图例。

figprops = dict(figsize=(gsp.plotSizeX, gsp.plotSizeY), dpi=gsp.plotSizeDPI, facecolor='#FFFFFF', edgecolor='#000000', linewidth=0.5, frameon=True)
fig = plt.figure(**figprops)
fig.autofmt_xdate(bottom = 0.2, rotation = 30, ha='right')
canvas = FigureCanvas(fig)

legHandles = []
legText = []
font= FontProperties(size='xx-small')

hostPlots = []

pcount = len(gsp.subPlots)
pn = 111

if pcount > 1:
   for i in range(1, pcount + 1):
      pn = pcount * 100 + (1 * 10) + i
      if i > 1:
          hostPlots.append(host_subplot(pn, axes_class = AA.Axes, sharex = hostPlots[0]))
      else:
          hostPlots.append(host_subplot(pn, axes_class = AA.Axes))
else:
   hostPlots.append(host_subplot(pn, axes_class = AA.Axes))

adjustprops = dict(bottom=0.10, right=0.70, wspace=0.2, hspace=0.2)       # Subplot properties
fig.subplots_adjust(**adjustprops)

spno = 0

for sp in gsp.subPlots:
   series = 0
   axes_offset = 0

   minTicks = sp.axes[0].minTicks
   majTicks = sp.axes[0].majTicks

   for a in sp.axes:
      if series == 0:
         ax = hostPlots[sp.plotNo-1]
         p, = ax.plot_date(plotDate, y, 'k', linewidth = a.lineWidth, color = a.lineColor, label = a.axesLabel, linestyle = a.lineType)

         h, l = ax.get_legend_handles_labels()
         legHandles.append(h)
         legText.append(a.axesLegend)
      else:   # if plotType = 'paraPlot' or plotType = 'subPlot'
         ax = hostPlots[sp.plotNo-1].twinx()
         p, = ax.plot_date(plotDate, y, 'k', linewidth = a.lineWidth, color = a.lineColor, label = a.axesLabel, linestyle = a.lineType)

         h, l = ax.get_legend_handles_labels()
         legHandles.append(h)
         legText.append(a.axesLegend)

         trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
         rect = patches.Rectangle((dtn, 0), width=1, height=1,transform=trans, color='yellow', alpha=0.333)

         ax.add_patch(rect)
      series += 1
   spno += 1

   pl = hostPlots[spno-1]
   box = pl.get_position()
   pl.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.85])

ax = hostPlots[0]
ax.set_title( gsp.plotName)  #'Time series Data from CWMS Web Service')

if gsp.tightLayout:
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

figlegend(legHandles, (legText,), 'upper center', bbox_to_anchor=(0.40, 0.07), prop=font, ncol=2)

get_legend_handles_labels()使用and升级后是否有其他人看到/修复了相同的问题figlegend()

4

1 回答 1

0

这表现如预期:

ax = gca()
plot(range(15), label='test')
h, l = ax.get_legend_handles_labels()
figlegend(h, l, 'upper center')

上主。我怀疑您的代码中还有其他问题

 legText.append(a.axesLegend)

那条线

于 2013-08-14T01:18:18.337 回答