Matplotlib equivalent of pygame flip

前端 未结 4 528
天涯浪人
天涯浪人 2021-01-05 05:21

I have a program with rapid animations which works perfectly under pygame, and for technical reasons, I need to do the same using only matplotlib or an other widespread modu

4条回答
  •  一生所求
    2021-01-05 05:53

    Given you talked about using widespread modules, here's a proof of concept using OpenCV. It runs pretty fast here, up to 250-300 generated frames per second. It's nothing too fancy, just to show that maybe if you're not using any plotting feature matplotlib shouldn't really be your first choice.

    import sys                                                                                 
    import time                                                                                
    import numpy as np                                                                         
    import cv2                                                                                 
    
    if sys.version_info >= (3, 3):                                                             
        timer = time.perf_counter                                                              
    else:                                                                                      
        timer = time.time                                                                      
    
    def f(x, y):                                                                               
        return np.sin(x) + np.cos(y)                                                           
    
    # ESC, q or Q to quit                                                                      
    quitkeys = 27, 81, 113                                                                     
    # delay between frames                                                                     
    delay = 1                                                                                  
    # framerate debug init                                                                     
    counter = 0                                                                                
    overflow = 1                                                                               
    start = timer()                                                                            
    
    x = np.linspace(0, 2 * np.pi, 400)                                                         
    y = np.linspace(0, 2 * np.pi, 400).reshape(-1, 1)                                          
    
    while True:                                                                                
        x += np.pi / 15.                                                                       
        y += np.pi / 20.                                                                       
    
        cv2.imshow("animation", f(x, y))                                                       
    
        if cv2.waitKey(delay) & 0xFF in quitkeys:                                              
            cv2.destroyAllWindows()                                                            
            break                                                                              
    
        counter += 1                                                                           
        elapsed = timer() - start                                                              
        if elapsed > overflow:                                                                 
            print("FPS: {:.01f}".format(counter / elapsed))                                    
            counter = 0                                                                        
            start = timer()                                                                                                
    

提交回复
热议问题