如何在 Spyder/Ipython/matplotlib 中重新获取交互式绘图?


要获得交互式绘图,我们需要激活图形。使用 plt.ioff() 和 plt.ion(),我们可以对绘图执行交互式操作。

步骤

  • 使用子图方法创建 fig 和 ax 变量,其中默认 nrows 和 ncols 为 1。

  • 使用 plot() 方法绘制一条线。

  • 设置线条的颜色,即橙色。

  • 使用 plt.ioff() 方法停止交互。

  • 要制作交互式绘图,请更改线条坐标的颜色。

  • 使用 plt.ion() 方法开始交互。

示例

在 Ipython 中使用交互式绘图 -

In [1]: %matplotlib auto

Using matplotlib backend: GTK3Agg

In [2]: import matplotlib.pyplot as plt

In [3]: fig, ax = plt.subplots()    # Diagram will get popped up, Let’s interact.

In [4]: ln, = ax.plot(range(5))     # Drawing a line

In [5]: ln.set_color('orange')      # Changing drawn line to orange

In [6]: plt.ioff()                  # Stopped interaction

In [7]: ln.set_color('green')
# Since we have stopped the interaction in the last step

In [8]: plt.ion()                   # Started interaction

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

输出

更新于: 2021-03-15

3K+ 浏览

开启您的 职业生涯

通过完成课程获得认证

入门
广告