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 badges1 Answer
Reset to default -1To increase the size of the figure, I see 2 ways:
- Use the parameter
figsize
e.g.figsize=(12, 4)
insideplt.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
版权声明:本文标题:matplotlib - How can I make an ipympl plot stretch to fit its canvas width? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738487113a2089484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论