ipympl icon indicating copy to clipboard operation
ipympl copied to clipboard

Nesting into HBox collapses plot width

Open JazzTap opened this issue 6 years ago • 1 comments

Here's another effect of requiring the canvas size explicitly, but somehow isn't visible unless the plot gets dropped into an ipywidgets layout (e.g. by a downstream package).

If the interactive matplotlib is the child of one p-Panel, it's fine. But gets squashed as the child of two p-Panels (regardless of orientation).

Explicitly setting canvas.layout.width fixes it, and propagates to the front-end as expected.

from ipywidgets import HTML, HBox, VBox
from matplotlib.figure import Figure
from ipympl.backend_nbagg import Canvas, FigureManager

figure = Figure()
canvas = Canvas(figure)
canvas.manager = FigureManager(canvas, 0)

axes = figure.add_subplot(1,1,1)
HBox([HBox([HTML(''), canvas])])
HBox([canvas])

JazzTap avatar Jul 13 '19 17:07 JazzTap

I experience same, though the degree of nesting of V and HBox and Tabs required to reproduce is different to @JazzTap The following error is also raised:

ValueError                                Traceback (most recent call last)
~/.local/share/virtualenvs/goldmine-hCKJOvFR/lib/python3.6/site-packages/ipympl/backend_nbagg.py in _handle_message(self, object, content, buffers)
    210             self.manager.resize(w, h)
    211         else:
--> 212             self.manager.handle_json(content)
    213 
    214     def send_json(self, content):

~/.local/share/virtualenvs/goldmine-hCKJOvFR/lib/python3.6/site-packages/matplotlib/backends/backend_webagg_core.py in handle_json(self, content)
    438 
    439     def handle_json(self, content):
--> 440         self.canvas.handle_event(content)
    441 
    442     def refresh_all(self):

~/.local/share/virtualenvs/goldmine-hCKJOvFR/lib/python3.6/site-packages/matplotlib/backends/backend_webagg_core.py in handle_event(self, event)
    238         handler = getattr(self, 'handle_{0}'.format(e_type),
    239                           self.handle_unknown_event)
--> 240         return handler(event)
    241 
    242     def handle_unknown_event(self, event):

~/.local/share/virtualenvs/goldmine-hCKJOvFR/lib/python3.6/site-packages/matplotlib/backends/backend_webagg_core.py in handle_resize(self, event)
    318         fig = self.figure
    319         # An attempt at approximating the figure size in pixels.
--> 320         fig.set_size_inches(x / fig.dpi, y / fig.dpi, forward=False)
    321 
    322         _, _, w, h = self.figure.bbox.bounds

~/.local/share/virtualenvs/goldmine-hCKJOvFR/lib/python3.6/site-packages/matplotlib/figure.py in set_size_inches(self, w, h, forward)
    900         size = np.array([w, h])
    901         if not np.isfinite(size).all() or (size <= 0).any():
--> 902             raise ValueError(f'figure size must be positive finite not {size}')
    903         self.bbox_inches.p1 = size
    904         if forward:

ValueError: figure size must be positive finite not [0. 0.]

ipympl=0.56

dschofield avatar Aug 06 '20 13:08 dschofield