Skip to content
Related Articles

Related Articles

Improve Article

Matplotlib.figure.Figure.ginput() in Python

  • Last Updated : 30 Apr, 2020
Geek Week

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot elements.

matplotlib.figure.Figure.ginput() method

The ginput() method figure module of matplotlib library is used to block call to interact with a figure.

Syntax: ginput(self, n=1, timeout=30, show_clicks=True, mouse_add=1, mouse_pop=3, mouse_stop=2)

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

  • n : This parameter is the number of mouse clicks to accumulate.
  • timeout : This parameter is the number of seconds to wait before timing out.
  • show_clicks : This parameter is used to show a red cross at the location of each click.
  • mouse_add : This parameter is the Mouse button used to add points.
  • mouse_pop : This parameter is the Mouse button used to remove the most recently added point.
  • mouse_stop : This parameter is the Mouse button used to stop input.

Returns: This method return the list of the clicked (x, y) coordinates.



Below examples illustrate the matplotlib.figure.Figure.ginput() function in matplotlib.figure:

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
  
  
t = np.arange(10)
  
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
  
ax.plot(t, np.sin(t))
    
fig.suptitle('matplotlib.figure.Figure.ginput() \
function Example', fontweight ="bold")
  
print("After 3 clicks :")
x = fig.ginput(3)
print(x)
  
plt.show()

Output:

After 3 clicks :
[(5.370117187499999, 0.12683733876216197), 
(5.370117187499999, 0.12683733876216197),
(5.370117187499999, 0.12683733876216197)]

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
  
  
np.random.seed(10**7)
    
x1 = np.random.rand(103, 53
fig = plt.figure(dpi = 100)
axes = fig.add_subplot(111)
    
fig.suptitle('matplotlib.figure.Figure.ginput() \
function Example', fontweight ="bold")
  
print("After 2 clicks :")
axes.imshow(x1)
x = fig.ginput(2
print(x)
  
plt.show()

Output:

After 2 clicks :
[(29.90151515151514, 65.65854978354977),
(29.90151515151514, 65.65854978354977)]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :