matplotlib.pyplot.disconnect函数
Matplotlib是Python中的一个库,它是NumPy库的数值-数学扩展。Pyplot是一个基于状态的Matplotlib模块接口,该模块提供了一个类似matlab的接口。
matplotlib.pyplot.disconnect()函数
使用matplotlib库pyplot模块中的disconnect()函数来断开id为cid的回调函数的连接。
语法:matplotlib.pyplot.disconnect (cid)
参数:该方法接受如下参数说明:
- cid:该参数用于断开回拨。
下面的例子演示了matplotlib.pyplot.disconnect()函数在matplotlib.pyplot中的作用:
示例1
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(np.random.rand(10))
def onclick(event):
print('% s click: button =% d, x =% d, y =% d,\
xdata =% f, ydata =% f' %
('double' if event.dblclick else 'single',
event.button,
event.x,
event.y,
event.xdata,
event.ydata))
cid = fig.canvas.mpl_connect('button_press_event',
onclick)
fig.canvas.mpl_disconnect(cid)
plt.suptitle('matplotlib.pyplot.disconnect()\
function Example', fontweight ="bold")
plt.show()
输出:
示例2
# Implementation of matplotlib function
from matplotlib.backend_bases import MouseButton
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
def gfg1(event):
# get the x and y pixel coords
x, y = event.x, event.y
if event.inaxes:
# the axes instance
ax = event.inaxes
print('Coordinates : % f and\
% f' % (event.xdata, event.ydata))
def gfg2(event):
if event.button is MouseButton.LEFT:
print('Successfully disconnected')
plt.disconnect(binding_id)
binding_id = plt.connect('motion_notify_event',
gfg1)
plt.connect('button_press_event', gfg2)
plt.suptitle('matplotlib.pyplot.disconnect()\
function Example', fontweight ="bold")
plt.show()
输出:
点击图后的背景结果 :