Python中的 Matplotlib.pyplot.waitforbuttonpress()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是 Matplotlib 模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。在 Pyplot 中可以使用各种图,包括线图、等高线图、直方图、散点图、3D 图等。
matplotlib.pyplot.waitforbuttonpress() 方法
matplotlib 库的 pyplot 模块中的waitforbuttonpress() 方法用于阻止调用与图形交互。
Syntax: matplotlib.pyplot.waitforbuttonpress(timeout=- 1)
Parameters: This method accept the following parameters that are discussed below:
- timeout: This parameter is the timeout value.
Returns: This method does not returns any value.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.waitforbuttonpress()函数:
示例 1:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
for ite in range(2):
x = np.linspace(-2, 6, 100)
y = (ite + 1)*x
fig = plt.figure()
ax = fig.subplots()
ax.plot(x, y, '-b')
fig.suptitle("""matplotlib.pyplot.waitforbuttonpress()
function Example\n\n""", fontweight ="bold")
w = plt.waitforbuttonpress()
print("Result after", ite, "click", w)
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.subplots()
def tellme(s):
fig.suptitle(s, fontweight ="bold")
fig.canvas.draw()
renderer = fig.canvas.renderer
fig.draw(renderer)
plt.clf()
ax.axis([-1., 1., -1., 1.])
plt.setp(plt.gca(), autoscale_on = False)
tellme("""matplotlib.pyplot.waitforbuttonpress()
function Example\n\n""")
w = plt.waitforbuttonpress()
print("Result after click :", w)
plt.show()
输出: