0

I am trying to follow some of the guides for generating real-time plots such as: real-time plotting in while loop with matplotlib and http://thread.gmane.org/gmane.comp.python.matplotlib.general/35705

However, I believe the sample code was compiled with python 2.7. When I try to compile mine I do not see a real-time plot being updated. Is this because python 3 doesn't support it? Or am I missing a library or something? Only when I stop the while loop I see the last value that was plotted. I am using Rodeo as my IDE; would this be preventing me from viewing the real-time plot?

import serial 
import numpy as np
import matplotlib.pyplot as plt

def plotlive():
    plt.plot(ard_dat,'o')
    plt.xlabel('count', fontsize=12)
    plt.ylabel('reading', fontsize=12)
    plt.legend(loc='upper right')
ard_dat=[]
plt.ion()
cnt=0
arduinoSerialData = serial.Serial('com5',9600)

while True:
    while (arduinoSerialData.inWaiting()==0):
        pass 

        srdata = arduinoSerialData.readline()
        try:
            intstrdata = int(srdata)
        except ValueError:
            pass 
        ard_dat.append(intstrdata)
        drawnow(plotlive)
        plt.pause(.00001) 
        cnt+=1
        if (cnt>50):
            ard_dat.pop(0)
Spencer Trinh
  • 743
  • 12
  • 31

1 Answers1

0

There is no specific python 2 or 3 command in the code so you can leave that out of the equation.

I would not recommend to use drawnow. Call plotlive() directly instead. This is however just a recommendation because drawnow is a pretty useless package, but it would not prevent the code from running.

Assuming that the serial works fine, the code from the question should produce an updating plot when being run as script.

The main point is this: Rodeo is not capable of producing animations. See this issue: https://github.com/yhat/rodeo/issues/488 The reason is that it uses a notebook-like output mechanism. While in Jupyter notebook you would actually be able to set the backend to interactive mode (%matplotlib tk or %matplotlib notebook), this is apparently not possible in Rodeo.

Rodeo also does not seem to have the option to run some code as python script outside of its IDE. Therefore the idea would be to either use a different IDE or to at least run animations outside of Rodeo.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • great, thanks for the info. I am new to jupyter so have to do some reading and experimenting. i'm new to this concept of backend interactive mode. if you know of any beginning resources please let me know. – Spencer Trinh Jul 29 '17 at 23:47
  • I tried adding %matplotlib notebook at the top and ran the same code in jupyter notebook without drawnow, and it doesn't do anything. I was able to output a plot of 40 values extracted from the arduino however, so the plotting seems to work (if it is a static list), something in my code is not? – Spencer Trinh Jul 30 '17 at 00:10
  • Sorry, while I did say that the code from the question would probably run when executed as script, I'm not sure that it would in Jupyter. What I said was that in general animations can be run in Jupyter. As a first step, run your code as a script, see if it works. If you then want to also be able to run it in jupyter, one may need to adapt it. – ImportanceOfBeingErnest Jul 30 '17 at 01:36
  • I used this code and it works in jupyter. https://gist.github.com/brandoncurtis/33a67d9d402973face8d Thank you for steering me in the right direction. – Spencer Trinh Jul 30 '17 at 05:16