HomeLanguagesMatplotlib.pyplot.disconnect() in Python

Matplotlib.pyplot.disconnect() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.disconnect() Function

The disconnect() function in pyplot module of matplotlib library is used to disconnect the callback with id cid.

Syntax: matplotlib.pyplot.disconnect(cid)

Parameters: This method accept the following parameters that are described below:

  • cid: This parameter is used to disconnect the callback.

Below examples illustrate the matplotlib.pyplot.disconnect() function in matplotlib.pyplot:

Example #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()


Output:

Example #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 : % 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()


Output:
Figure shown

Background result after click on figure

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32541 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6920 POSTS0 COMMENTS
Nicole Veronica
12036 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12143 POSTS0 COMMENTS
Shaida Kate Naidoo
7055 POSTS0 COMMENTS
Ted Musemwa
7292 POSTS0 COMMENTS
Thapelo Manthata
7014 POSTS0 COMMENTS
Umr Jansen
7000 POSTS0 COMMENTS