Python - Real Time Plotting
<p>I wrote a simple python script that display my sensor data (read from Serial).
I wish to understand why the figure crashes when I'm clicking anywhere... 
Is there any - simple way, if possible - to avoid the script to crash if I try to do something else on the computer? </p>

<p>Moreover, if I want to develop a simple GUI, has someone an example to learn how to do it? Does it will avoid this problem ?</p>

<pre><code>import serial
import re
import numpy as np
import time
from matplotlib import pyplot as plt

SERIAL_PORT = 'COM3'
BAUDRATE = 9600
BUFFER_SIZE = 30



print("initialisation ...")
ser = serial.Serial(SERIAL_PORT, BAUDRATE)
buffer = []

plt.ion()
fig = plt.figure()
ydata = [0]*BUFFER_SIZE*2
prevBuf = [0]*BUFFER_SIZE*10
ax1=plt.axes()

line, = plt.plot(prevBuf)
plt.ylim([2000,10000])


def clean_serial():
    i=0
    while i&lt;=5:
        data = ser.readline()
        print data
        i+=1
    ser.flush()


def extract_number(rawdata):
    return float(re.sub("[^0-9.]", " ", rawdata))

def process_buffer(buf):

    prevBuf[len(prevBuf)-BUFFER_SIZE:] = buf

    line.set_xdata(np.arange(len(prevBuf)))
    line.set_ydata(prevBuf)

    ydataMax = max(prevBuf)+10
    ydataMin = min(prevBuf)-100
    plt.ylim([ydataMin,ydataMax])
    plt.draw()


    prevBuf[:] = prevBuf[BUFFER_SIZE:]+buf

print("cleaning serial")
clean_serial()

print("reading value from serial")
while True:
    data = extract_number(ser.readline())
    buffer.append(data)
    if len(buffer)&gt;= BUFFER_SIZE:
        process_buffer(buffer)
        buffer=[]


ser.close()
</code></pre>

<p>which output : <a href="http://i.stack.imgur.com/1EUhx.jpg" rel="nofollow">script crashing</a></p>

<p>Thank you ! </p>