matplotlib之pyplot模块——交互式绘图模式管理(ion()、ioff()、isinteractive())

本文详细介绍了matplotlib的交互式绘图模式,包括如何开启和关闭,以及其在不同环境下的表现。交互模式允许用户在图形窗口不关闭的情况下实时更新图形,而无需每次都调用show()函数。同时,文章提到了IPython和Python shell对于交互绘图的支持,并提供了相关源码示例来说明交互式绘图的工作原理。

当前有效matplotlib版本为:3.4.1

交互模式

matplotlib使用交互式后端时,可实现交互式绘图。

如果处于交互模式,新创建的图形将会立刻显示,修改图形(即运行新的绘图语句)时原图形会立即重绘,matplotlib.pyplot.show()不会阻塞显示。此时shell可以输入新的命令。

如果处于非交互模式,新创建的图形或修改图形时,图形不会立即显示(当执行matplotlib.pyplot.show()时才会显示),matplotlib.pyplot.show()会阻塞显示。此时shell不可以输入新的命令。

简短来说,交互模式最大的特点即每执行一个绘图命令就会重绘一次图形。

注意!交互模式不单需要切换至支持交互模式的后端,而且需要在特定的REPL shell中运行,官网明确指出支持IPythonpython shell,不支持IDLE。如果想让jupyter notebookjupyter lab支持交互绘图模式,需要安装ipympl包(原名jupyter-matplotlib)。

交互模式相关函数

交互式绘图模式的开关由运行时参数interactive控制,值类型为布尔值,默认值为False

matplotlib.pyplot模块提供了isinteractiveionioff等函数对交互式绘图模式进行管理。

  • matplotlib.pyplot.isinteractive():查询交互式绘图模式状态。
  • matplotlib.pyplot.ion():开启交互式绘图模式。
  • matplotlib.pyplot.ioff():关闭交互式绘图。
  • matplotlib.pyplot.install_repl_displayhook():安装matplotlib显示钩子,当控制权转移到shell时,会立即重绘图形,仅支持IPython和传统python shell。
  • matplotlib.pyplot.uninstall_repl_displayhook():卸载matplotlib显示钩子。

相关源码

matplotlib

def is_interactive():
    return rcParams['interactive']
    
def interactive(b):
    rcParams['interactive'] = b
matplotlib.pyplot模块
def isinteractive():
    return matplotlib.is_interactive()

def ioff():
    return _IoffContext()

def ion():
    return _IonContext()

class _IoffContext:

    def __init__(self):
        self.wasinteractive = isinteractive()
        matplotlib.interactive(False)
        uninstall_repl_displayhook()

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_value, traceback):
        if self.wasinteractive:
            matplotlib.interactive(True)
            install_repl_displayhook()
        else:
            matplotlib.interactive(False)
            uninstall_repl_displayhook()


class _IonContext:

    def __init__(self):
        self.wasinteractive = isinteractive()
        matplotlib.interactive(True)
        install_repl_displayhook()

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_value, traceback):
        if not self.wasinteractive:
            matplotlib.interactive(False)
            uninstall_repl_displayhook()
        else:
            matplotlib.interactive(True)
            install_repl_displayhook()

案例:演示交互式绘图

Python Shell中

  • 导入matplotlib.pyplot模块。
  • 使用matplotlib.pyplot.ion()函数开启交互绘图模式。
  • 输入plt.plot([1,1])绘制直线,命令执行完后立即弹出窗口显示绘制结果。
  • 在窗口不关闭的情况下,可以在shell中继续输入plt.title("123")绘制标题,窗口中图形会自动重绘添加标题。
  • 注意:关闭绘图窗口并不会关闭交互绘图模式。关闭绘图窗口之后,再次执行绘图命令相当于绘制一个新的图形。

退出Python Shell将会关闭交互绘图模式。

通过对比可知:交互式绘图模式执行绘图命令,不执行show()就会立即显示图形,随后执行其他绘图命令会立即修改图形。非交互式绘图模式,执行完绘图命令不会立即显示图形,只有执行show()才会显示图形。
在这里插入图片描述
在非交互绘图模式下,只有执行了show函数后才会显示绘图窗口,而且shell处于阻塞状态,无法输入新绘图命令。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值