Open In App

Matplotlib.pyplot.disconnect() in Python

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads