matplotlib.pyplot.waitforbuttonpress()函数
matplotlib.pyplot.waitforbuttonpress()方法
matplotlib库pyplot模块中的waitforbuttonpress()方法用于阻塞调用,以与图形交互。
语法:matplotlib.pyplot.waitforbuttonpress(超时= – 1)
参数:该方法接受以下参数:
- timeout:超时值。
返回:此方法不返回任何值。
下面的例子演示了matplotlib.pyplot.waitforbuttonpress()函数在matplotlib.pyplot中的作用:
示例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()
输出: