1

I'm trying to show real-time graphs(dynamic plotting) using python. However, the result didn't show in a single one graph, but generate a new one each second, which didn't mean updating live graph. How can I solve it? Is there any problem in my code?

import serial
import time
import matplotlib.pyplot as plt
from drawnow import drawnow

DataList = []
pcs = serial.Serial('COM4', baudrate = 9600, timeout = 1)
time.sleep(3)
plt.ion()

def makeFig():
    plt.plot(DataList, 'rd-')

def getValues():
    pcs.write(b"MEASure:VOLTage:DC?\n")
    pcsData = pcs.readline().decode('ascii').split('\n\r')
    DataList.append(float(pcsData[0]))


while(1):
    getValues()
    drawnow(makeFig)
    plt.pause(.000001)

The result snapshot: enter image description here

livemyaerodream
  • 890
  • 6
  • 19

1 Answers1

1

the drawnow called in this way is designed as one-shot draw. See if drawnow(caller, show_once=True) corrects your problem, else you may need to look elsewhere - such as using alternative plotting functions:

How do I plot in real-time in a while loop using matplotlib?

Glycerine
  • 7,157
  • 4
  • 39
  • 65