Computer >> Computer tutorials >  >> Programming >> Python

Programmatically Stop Interaction for specific Figure in Jupyter notebook


To programmatically stop interaction for specific figures in Jupyter notebook, we can use plt.off() to stop any interaction after that.

Execute the following commnads sequentially −

  • %matplotlib auto
  • import matplotlib.pyplot as plt
  • plt.rcParams["figure.figsize"] = [7.50, 3.50]
  • plt.rcParams["figure.autolayout"] = True
  • Create a figure and a set of subplots.
  • Plot the line on the axis (From step 5).
  • Turn off the interaction.
  • To display the figure, use show() method.

Example

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]:

Output

Programmatically Stop Interaction for specific Figure in Jupyter notebook