Python中的 Matplotlib.figure.Figure.ginput()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。
matplotlib.figure.Figure.ginput() 方法
matplotlib 库的 ginput() 方法图形模块用于阻止调用与图形交互。
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.
下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.ginput()函数:
示例 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()
输出:
After 3 clicks :
[(5.370117187499999, 0.12683733876216197),
(5.370117187499999, 0.12683733876216197),
(5.370117187499999, 0.12683733876216197)]
示例 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()
输出:
After 2 clicks :
[(29.90151515151514, 65.65854978354977),
(29.90151515151514, 65.65854978354977)]