5

我正在构建一个基于JupyterVoila的带有交互式绘图的 webapp 。在笔记本内部,我使用ipywidgets来处理交互性和%matplotlib widget后端,因此我可以更新我的绘图而无需重新绘制整个图形(在 Voila 中不起作用%matplotlib inline并且%matplotlib notebook不会渲染)。

我的问题是我无法设置图形大小,使其与其画布或容器的大小相匹配,这些画布或容器在窗口调整大小时会自动调整大小。

这是我的情况的一个小例子:

%matplotlib widget
from IPython.display import display
import matplotlib.pyplot as plt
import ipywidgets as widgets
import numpy as np

# Output widget that will contain the figure
out = widgets.Output(layout = {
    'border': '1px solid black',
    # 'width': '100%' # Not needed and does not help
})

# Initializing the figure within out
with out:
    fig, ax = plt.subplots(1, constrained_layout=True)
    fig.set_size_inches(7,5) # Is not automatic and stays the same on window resize
    fig.canvas.layout.border = '1px solid red'
    # fig.canvas.layout.width = '100%' # Not needed and does not help


# Func for updating the data in ax within out
def update_ax(slider):
    with out:
        plt.cla() # Clearing the ax
        ax.plot(np.arange(10), np.random.random(10)* slider)

slider = widgets.FloatSlider(10)      
display(slider)
iplot = widgets.interactive(update_ax, slider=slider)
iplot.update()
display(out)

这是输出: 上面代码的输出

我希望图形(蓝色矩形)跨越画布(红色矩形)的 100% 宽度,并且我希望它自动调整大小,因为画布宽度随着输出容器(黑色矩形)的变化而变化,输出容器(黑色矩形)随着窗口宽度的变化而变化。例如,当我手动调整窗口大小时。

我认为这一定是可能的,因为角落(绿色圆圈)会调整大小,但只是基于手动用户输入,而不是窗口宽度的变化。不错的解决方案是这样的 'fig.set_size_relative('100%', 'auto')',但这显然没有实现。

非常感谢任何帮助,我已经为此浪费了比健康更多的时间:)

4

0 回答 0