如何使用 jupyter 避免 matplotlib 图像周围的框?

How to avoid the box around a matplotlib image with jupyter?

在 jupyterhub notebook (python 3.8.5) 上并使用 matplotlib(版本 3.3.2)我正在尝试创建一个简单的绘图。单元格内容如下:

import matplotlib.pyplot as plt

def plot_something(time_array, dependent_var, label="label"):
    plt.plot(time_array, dependent_var, label=label)
    plt.xlabel('Time (ms)')
    plt.ylabel(label)
plot_something([1,2,3], [4,5,6])

但是,在创建的图像周围绘制了一个奇怪的滚动框,右侧有一个滚动条(即您无法看到整个图像,您必须滚动!):

同一笔记本中的其他 matplotlib 图不会发生这种情况。该图像显示了完整的输出单元格(您可以看到绘图的上部缺失。您需要向上滚动才能看到它)。

如何以编程方式避免 matplotlib 图周围出现这个奇怪的方框?

P.S。我不确定如何重现此问题。如果我在笔记本上执行代码它工作正常,我得到的只是没有那个奇怪的滚动框的情节:

我还检查了这两种情况在实际显示的 html 代码中的差异。我没有任何区别!

那是因为滚动。要更改它,您可以转到 Cell > All Outputs > Toggle Scrolling 或其他选项之一,直到获得所需的结果。

作为一个既不需要额外代码也不需要在笔记本中进行用户交互的实际更好的解决方案是检查笔记本本身的元数据。显示滚动框的单元格元数据如下所示

{
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plot_timecourse(time, v_soma)"
   ]
  },

把滚动部分去掉,即改成

{
   "cell_type": "code",
   "execution_count": null,
   "metadata": {}
   "outputs": [],
   "source": [
    "plot_timecourse(time, v_soma)"
   ]
  },