以编程方式停止 Jupyter 笔记本中特定图形的交互

pythonserver side programmingprogramming更新于 2025/8/8 11:24:17

要以编程方式停止 Jupyter 笔记本中特定图形的交互,我们可以使用 plt.off() 来停止此后的任何交互。

按顺序执行以下命令 −

  • %matplotlib auto
  • import matplotlib.pyplot as plt
  • plt.rcParams["figure.figsize"] = [7.50, 3.50]
  • plt.rcParams["figure.autolayout"] = True
  • 创建一个图形和一组子图。
  • 在轴上绘制线(来自步骤 5)。
  • 关闭交互。
  • 要显示图形,请使用 show() 方法。

示例

In [1]: %matplotlib auto
Using matplotlib backend: Qt5Agg

In [2]: import matplotlib.pyplot as plt

In [3]: plt.rcParams["figure.figsize"] = [7.50, 3.50]
   ...: plt.rcParams["figure.autolayout"] = True

In [4]: fig, ax = plt.subplots()

In [5]: ax.plot([2, 4, 7, 5, 4, 1])
Out[5]: [<matplotlib.lines.Line2D at 0x7fa6e00f5e48>]

In [6]: plt.ioff()
In [7]: ax.plot([1, 1, 2, 1, 2, 2])
Out[7]: [<matplotlib.lines.Line2D at 0x7fa6e0063ef0>]

In [8]: fig.show()

In [9]:

输出


相关文章


有用资源