Reputation: 11768
I have a simple Jupyter notebook containing a matplotlib
plot rendered as an ipywidget
using ipympl
. I would like to have each plot stretch to the width of the viewport.
The ipympl
documentation says that, given a figure object fig
, fig.canvas
should be a "proper Jupyter interactive widget". According to the documentation for layout of Jupyter widgets, it seems like I should be able to make the canvas stretch to fit the width of the viewport by setting its layout.width
to 100%
. So, I'm trying the below code:
import numpy as np
from matplotlib import pyplot as plt
%matplotlib widget
fig, ax = plt.subplots(tight_layout = True)
ax.plot(np.arange(10))
fig.canvas.layout.width = '100%'
fig.canvas.toolbar_position = 'bottom'
This yields the following result:
What this looks like to me is that the widget canvas did properly stretch to fit the viewport, because the figure toolbar is now all the way over to the right. However, the figure is still its original size (640x480 pixels, to be exact).
How can I make the figure stretch to fill the widget canvas that contains it programmatically? If I have to manually synchronize the two sizes in some way, I can do that, but in looking through the properties of the canvas, I haven't been able to identify any way to get its current actual size (in pixels) so that I can resize the figure accordingly.
Upvotes: 0
Views: 25
Reputation: 2070
To increase the size of the figure, I see 2 ways:
figsize
e.g. figsize=(12, 4)
inside plt.subplots()
Upvotes: -1