當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Matplotlib.pyplot.disconnect()用法及代碼示例

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。 Pyplot是Matplotlib模塊的基於狀態的接口,該模塊提供了MATLAB-like接口。

matplotlib.pyplot.disconnect()函數

matplotlib庫的pyplot模塊中的disconnect()函數用於斷開ID為cid的回調。

用法: matplotlib.pyplot.disconnect(cid)


參數:此方法接受以下描述的參數:

  • 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()

輸出:
如圖所示

單擊圖後的背景結果




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.pyplot.disconnect() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。