當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Matplotlib.pyplot.waitforbuttonpress()用法及代碼示例

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。 Pyplot是Matplotlib模塊的基於狀態的接口,該模塊提供了MATLAB-like接口。可在Pyplot中使用的各種圖線圖,輪廓圖,直方圖,散點圖,3D圖等。

matplotlib.pyplot.waitforbuttonpress()方法

matplotlib庫的pyplot模塊中的waitforbuttonpress()方法用於阻止與圖形交互的調用。

用法:matplotlib.pyplot.waitforbuttonpress(timeout=- 1)


參數:此方法接受下麵討論的以下參數:

  • timeout:此參數是超時值。

返回值:此方法不返回任何值。

以下示例說明了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() 

輸出:





注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.pyplot.waitforbuttonpress() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。