Python 如何正确退出永不结束的live matplotlibplot

Python 如何正确退出永不结束的live matplotlibplot,python,matplotlib,serial-port,pyserial,Python,Matplotlib,Serial Port,Pyserial,我创建了一个基本的3D动画,显示了3D空间中的一个对象,坐标来自Arduino import serial from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import matplotlib as mpl def make_the_plot(port1, baud1, duration): plt.ion() mpl.rcParams['toolbar'] = 'None'

我创建了一个基本的3D动画,显示了3D空间中的一个对象,坐标来自Arduino

import serial
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import matplotlib as mpl

def make_the_plot(port1, baud1, duration):

    plt.ion()
    mpl.rcParams['toolbar'] = 'None'
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    port = port1 
    baudrate = baud1
    ser = serial.Serial(port, baudrate)

    realtime=0
    while ser and realtime<duration:

        raw = (ser.readline())
        splitted= raw.split()
        realtime=realtime+1
        #print (realtime, maxtime)
        if 'reading' in splitted:

            print("error in sensor reading")
            msg = "Error in sensor readng "+raw
            return msg  
        try:
            ax.cla()
            x=float(splitted[0])
            y=float(splitted[1])
            z=float(splitted[2])

            ax.scatter(x,y,z)
            ax.set_xlim([0, 100])
            ax.set_ylim([0, 100])
            ax.set_zlim([0, 100])
            #fig.suptitle("Title centered above all subplots", fontsize=14)
            ax.set_xlabel('x [cm]')
            ax.set_ylabel('y [cm]')
            ax.set_zlabel('z [cm]')
            plt.tight_layout()
            plt.draw()
            plt.pause(0.003)

        except:
            pass

        msg="time's up"

    print (msg)
    return msg    

if __name__ == '__main__':

    make_the_plot('COM7',9600,100)   
导入序列号
从mpl_toolkits.mplot3d导入axes3d
将matplotlib.pyplot作为plt导入
将matplotlib导入为mpl
def使_成为_图(端口1、波特1、持续时间):
plt.ion()
mpl.rcParams['toolbar']='None'
图=plt.图()
ax=图添加_子图(111,投影='3d')
端口=端口1
波特率=波特率1
ser=串行。串行(端口,波特率)
实时=0

而ser和realtime我可以使用线程来完成