📜  Python中的 Matplotlib.pyplot.disconnect()

📅  最后修改于: 2022-05-13 01:54:20.913000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.disconnect()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 PyplotMatplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。

matplotlib.pyplot.disconnect()函数

matplotlib库的pyplot模块中的disconnect()函数用于断开id为cid的回调。

下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.disconnect()函数:

示例 #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()

输出:
如图所示

点击图后的背景结果