Thanks to visit codestin.com
Credit goes to github.com

Skip to content

nbAgg backend does not allow saving figures as png #16721

Closed
@logan-pearce

Description

@logan-pearce

Bug report

Bug summary

When using the nbAgg backend in jupyter notebook (after calling %matplotlib notebook) calling plt.savefig and saving as png produces an error. Other backends, including %matplotlib inline, and other formats such as pdf, do not produce the error.

Code for reproduction

%matplotlib notebook
x = np.linspace(0,np.pi,1000)
y = np.sin(x)
plt.figure(figsize = (4,4))
plt.plot(x,y)
plt.savefig('plot.png',format='png')

Actual outcome

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in _wait_cursor_for_draw_cm(self)
   2772             try:
-> 2773                 self.set_cursor(cursors.WAIT)
   2774                 yield

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py in set_cursor(self, cursor)
    380         if cursor != self.cursor:
--> 381             self.canvas.send_event("cursor", cursor=cursor)
    382         self.cursor = cursor

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py in send_event(self, event_type, **kwargs)
    345     def send_event(self, event_type, **kwargs):
--> 346         self.manager._send_event(event_type, **kwargs)
    347 

AttributeError: 'NoneType' object has no attribute '_send_event'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-e2427ff289f2> in <module>()
      8 plt.figure(figsize = (4,4))
      9 plt.plot(x,y)
---> 10 plt.savefig('plot.png',format='png')
     11 #plt.show()

~/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
    721 def savefig(*args, **kwargs):
    722     fig = gcf()
--> 723     res = fig.savefig(*args, **kwargs)
    724     fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
    725     return res

~/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py in savefig(self, fname, transparent, **kwargs)
   2201             self.patch.set_visible(frameon)
   2202 
-> 2203         self.canvas.print_figure(fname, **kwargs)
   2204 
   2205         if frameon:

~/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
   2091                     orientation=orientation,
   2092                     bbox_inches_restore=_bbox_inches_restore,
-> 2093                     **kwargs)
   2094             finally:
   2095                 if bbox_inches and restore_bbox:

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args, **kwargs)
    512         }
    513 
--> 514         FigureCanvasAgg.draw(self)
    515         if pil_kwargs is not None:
    516             from PIL import Image

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    390         with RendererAgg.lock, \
    391              (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
--> 392               else nullcontext()):
    393             self.figure.draw(self.renderer)
    394             # A GUI class may be need to update a window using this draw, so

~/anaconda3/lib/python3.7/contextlib.py in __enter__(self)
    110         del self.args, self.kwds, self.func
    111         try:
--> 112             return next(self.gen)
    113         except StopIteration:
    114             raise RuntimeError("generator didn't yield") from None

~/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in _wait_cursor_for_draw_cm(self)
   2774                 yield
   2775             finally:
-> 2776                 self.set_cursor(self._lastCursor)
   2777         else:
   2778             yield

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py in set_cursor(self, cursor)
    379     def set_cursor(self, cursor):
    380         if cursor != self.cursor:
--> 381             self.canvas.send_event("cursor", cursor=cursor)
    382         self.cursor = cursor
    383 

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py in send_event(self, event_type, **kwargs)
    344 
    345     def send_event(self, event_type, **kwargs):
--> 346         self.manager._send_event(event_type, **kwargs)
    347 
    348 

AttributeError: 'NoneType' object has no attribute '_send_event'

Expected outcome

The figure should save to file with no terminal output. Which does occur when using a different backend. The following code is successful, and is identical to the above but with the inline backend:

%matplotlib inline
x = np.linspace(0,np.pi,1000)
y = np.sin(x)
plt.figure(figsize = (4,4))
plt.plot(x,y)
plt.savefig('plot.png',format='png')

Saving figures as png using nbAgg worked in all previous versions of matplotlib.

Matplotlib version

  • Operating system: MacOS Catalina version 10.15.3
  • Matplotlib version: 3.2.0
  • Matplotlib backend: nbAgg
  • Python version: 3.7.2
  • Jupyter notebook version: 6.0.3
  • Other libraries:
    Upgraded via pip.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GUI: nbaggRelease criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions