title: ValueError: Image size of _x_ pixels is too large
body: <p>Error:Whenever I add legend to the plot, it gives me this error:
  I don't understand why? It works perfectly fine without <code>ax.legend()</code>.</p>
  
  <p>(Only related post: <a href="https://github.com/matplotlib/matplotlib/pull/3451" rel="noreferrer">https://github.com/matplotlib/matplotlib/pull/3451</a>)</p>
  
  <pre><code>ValueError                                Traceback (most recent call last)
  /home/abhishek/anaconda2/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
      305                 pass
      306             else:
  --&gt; 307                 return printer(obj)
      308             # Finally look for special method names
      309             method = get_real_method(obj, self.print_method)
  
  /home/abhishek/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in &lt;lambda&gt;(fig)
      225 
      226     if 'png' in formats:
  --&gt; 227         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
      228     if 'retina' in formats or 'png2x' in formats:
      229         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
  
  /home/abhishek/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt, bbox_inches, **kwargs)
      117 
      118     bytes_io = BytesIO()
  --&gt; 119     fig.canvas.print_figure(bytes_io, **kw)
      120     data = bytes_io.getvalue()
      121     if fmt == 'svg':
  
  /home/abhishek/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
     2242                 orientation=orientation,
     2243                 bbox_inches_restore=_bbox_inches_restore,
  -&gt; 2244                 **kwargs)
     2245         finally:
     2246             if bbox_inches and restore_bbox:
  
  /home/abhishek/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
      543 
      544     def print_png(self, filename_or_obj, *args, **kwargs):
  --&gt; 545         FigureCanvasAgg.draw(self)
      546         renderer = self.get_renderer()
      547         original_dpi = renderer.dpi
  
  /home/abhishek/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)
      457         if __debug__: verbose.report('FigureCanvasAgg.draw', 'debug-annoying')
      458 
  --&gt; 459         self.renderer = self.get_renderer(cleared=True)
      460         # acquire a lock on the shared font cache
      461         RendererAgg.lock.acquire()
  
  /home/abhishek/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in get_renderer(self, cleared)
      474 
      475         if need_new_renderer:
  --&gt; 476             self.renderer = RendererAgg(w, h, self.figure.dpi)
      477             self._lastKey = key
      478         elif cleared:
  
  /home/abhishek/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in __init__(self, width, height, dpi)
       91         self.height = height
       92         if __debug__: verbose.report('RendererAgg.__init__ width=%s, height=%s'%(width, height), 'debug-annoying')
  ---&gt; 93         self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
       94         self._filter_renderers = []
       95 
  
  ValueError: Image size of 388x76538 pixels is too large. It must be less than 2^16 in each direction.
  
  &lt;matplotlib.figure.Figure at 0x7fb7fd06c090&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>    fig, ax = plt.subplots(figsize=(12,8))
      ax.margins(0.05) # Optional, just adds 5% padding to the autoscaling
      decision_region(ax)
      plot_predict(ax)
      ax.set_xlabel('x1')
      ax.set_ylabel('x2')
      ax.set_title('Hidden units: '+str(hidden_units))
      ax.legend()
  </code></pre>