tight_layout KeyError default, matplotlib widget

Viewed 130

Using jupyterlab, i receive a KeyError: 'Default' when using plt.tight_layout() in combination with %matplotlib widget. The following code reproduces the issue:

import matplotlib.pyplot as plt
import numpy as np

%matplotlib widget

x=np.linspace(0,10)
y=x**2
plt.plot(x,y)
plt.tight_layout()

The complete error message is the following:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py in _wait_cursor_for_draw_cm(self)
   3024             try:
-> 3025                 self.canvas.set_cursor(tools.Cursors.WAIT)
   3026                 yield

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_webagg_core.py in set_cursor(self, cursor)
    209         }, cursor=cursor)
--> 210         self.send_event('cursor', cursor=cursor)
    211 

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_webagg_core.py in send_event(self, event_type, **kwargs)
    391         if self.manager:
--> 392             self.manager._send_event(event_type, **kwargs)
    393 

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_webagg_core.py in _send_event(self, event_type, **kwargs)
    540         for s in self.web_sockets:
--> 541             s.send_json(payload)
    542 

~/anaconda3/lib/python3.8/site-packages/ipympl/backend_nbagg.py in send_json(self, content)
    180         if content['type'] == 'cursor':
--> 181             self._cursor = cursors_str[content['cursor']]
    182 

KeyError: 'wait'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_119035/3466922198.py in <module>
      7 y=x**2
      8 plt.plot(x,y)
----> 9 plt.tight_layout()

~/anaconda3/lib/python3.8/site-packages/matplotlib/pyplot.py in tight_layout(pad, h_pad, w_pad, rect)
   2300 @_copy_docstring_and_deprecators(Figure.tight_layout)
   2301 def tight_layout(*, pad=1.08, h_pad=None, w_pad=None, rect=None):
-> 2302     return gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
   2303 
   2304 

~/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py in tight_layout(self, pad, h_pad, w_pad, rect)
   3186                                "compatible with tight_layout, so results "
   3187                                "might be incorrect.")
-> 3188         renderer = _get_renderer(self)
   3189         with getattr(renderer, "_draw_disabled", nullcontext)():
   3190             kwargs = get_tight_layout_figure(

~/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py in _get_renderer(figure, print_method)
   1542                 figure.canvas._get_output_canvas(None, fmt), f"print_{fmt}")
   1543         try:
-> 1544             print_method(io.BytesIO())
   1545         except Done as exc:
   1546             renderer, = figure._cachedRenderer, = exc.args

~/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py in wrapper(*args, **kwargs)
   1641             kwargs.pop(arg)
   1642 
-> 1643         return func(*args, **kwargs)
   1644 
   1645     return wrapper

~/anaconda3/lib/python3.8/site-packages/matplotlib/_api/deprecation.py in wrapper(*inner_args, **inner_kwargs)
    410                          else deprecation_addendum,
    411                 **kwargs)
--> 412         return func(*inner_args, **inner_kwargs)
    413 
    414     DECORATORS[wrapper] = decorator

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args)
    538             *metadata*, including the default 'Software' key.
    539         """
--> 540         FigureCanvasAgg.draw(self)
    541         mpl.image.imsave(
    542             filename_or_obj, self.buffer_rgba(), format="png", origin="upper",

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    431         self.renderer = self.get_renderer(cleared=True)
    432         # Acquire a lock on the shared font cache.
--> 433         with RendererAgg.lock, \
    434              (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
    435               else nullcontext()):

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

~/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py in _wait_cursor_for_draw_cm(self)
   3026                 yield
   3027             finally:
-> 3028                 self.canvas.set_cursor(self._lastCursor)
   3029         else:
   3030             yield

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_webagg_core.py in set_cursor(self, cursor)
    208             backend_tools.Cursors.RESIZE_VERTICAL: 'ns-resize',
    209         }, cursor=cursor)
--> 210         self.send_event('cursor', cursor=cursor)
    211 
    212     def set_image_mode(self, mode):

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_webagg_core.py in send_event(self, event_type, **kwargs)
    390     def send_event(self, event_type, **kwargs):
    391         if self.manager:
--> 392             self.manager._send_event(event_type, **kwargs)
    393 
    394 

~/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_webagg_core.py in _send_event(self, event_type, **kwargs)
    539         payload = {'type': event_type, **kwargs}
    540         for s in self.web_sockets:
--> 541             s.send_json(payload)
    542 
    543 

~/anaconda3/lib/python3.8/site-packages/ipympl/backend_nbagg.py in send_json(self, content)
    179         # Change in the widget state?
    180         if content['type'] == 'cursor':
--> 181             self._cursor = cursors_str[content['cursor']]
    182 
    183         elif content['type'] == 'message':

KeyError: 'default'
0 Answers
Related