admin管理员组

文章数量:1194000

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.

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.

Share Improve this question edited Jan 23 at 23:02 Jason R asked Jan 23 at 14:54 Jason RJason R 11.7k9 gold badges56 silver badges85 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

To increase the size of the figure, I see 2 ways:

  • Use the parameter figsize e.g. figsize=(12, 4) inside plt.subplots()
  • There is a small triangle at the bottom right of the plot that you can drag to get the size you like.

本文标签: matplotlibHow can I make an ipympl plot stretch to fit its canvas widthStack Overflow