Python Forum
multiple coordinate systems, with real time plotting - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: multiple coordinate systems, with real time plotting (/thread-7930.html)



multiple coordinate systems, with real time plotting - Wenzel3200 - Jan-30-2018

Hi guys,

I have been trying to plot data in real time from a DHT11 sensor (temperature and humidity) connected with a arduino Uno. I managed to get a code with which I can make one coordinate system with two graphs (see code 1). I also managed to change the code a little bit, so I can make 2 coordinate systems now, with in each plotted one graph (see code 2). The problem with the second code is, when I run it, I get two coordinate systems which overlap each other constantly.
I am working on a project of plotting multiple graphs in different coordinate systems (real time).

code 1:

import serial # import Serial Library
import numpy  # Import numpy
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *

temperature= []
humidity=[]
arduinoData = serial.Serial('com5', 9600) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0

def makeFig(): #Create a function that makes our desired plot
    plt.ylim(10,30)                                 #Set y min and max values
    plt.title('My Live Streaming Sensor Data')      #Plot the title
    plt.grid(True)                                  #Turn the grid on
    plt.ylabel('Temp')                              #Set ylabels
    plt.plot(temperature, 'ro-', label='Degrees C')        #plot the temperature
    plt.legend(loc='upper left')                    #plot the legend
    plt2=plt.twinx()                                #Create a second y axis
    plt.ylim(0,100)                                 #Set limits of second y axis- adjust to readings you are getting
    plt2.plot(humidity, 'go-', label='humdity (%)') #plot pressure data
    plt2.set_ylabel('Humidity (%)')                 #label second y axis
    plt2.ticklabel_format(useOffset=False)          #Force matplotlib to NOT autoscale y axis
    plt2.legend(loc='upper right')#plot the legend


#def makeFig2():
    #plt.ylim(0,100)                                 #Set y min and max values
    #plt.title('My Live Streaming Sensor Data')      #Plot the title
    #plt.grid(True)                                  #Turn the grid on
    #plt.ylabel('Humidit')                              #Set ylabels
    #plt.plot(humidity, 'ro-', label='(%)')           #plot the temperature
    #plt.legend(loc='upper left')                    #plot the legend
    #plt2=plt.twinx()                                #Create a second y axis
    #plt.ylim(0,100)                                 #Set limits of second y axis- adjust to readings you are getting
    #plt2.plot(humidity, 'go-', label='humdity (%)') #plot pressure data
    #plt2.set_ylabel('Humidity (%)')                 #label second y axis
    #plt2.ticklabel_format(useOffset=False)          #Force matplotlib to NOT autoscale y axis
    #plt2.legend(loc='upper right')#plot the legend


#def makeFig3() :
    #plt.ylim(0,100)                                 #Set y min and max values
    #plt.title('My Live Streaming Sensor Data')      #Plot the title
    #plt.grid(True)                                  #Turn the grid on
    #plt.ylabel('Humidit')                              #Set ylabels
    #plt.plot(humidity, 'ro-', label='(%)')           #plot the temperature
    #plt.legend(loc='upper left')                    #plot the legend
    #plt2=plt.twinx()                                #Create a second y axis
    #plt.ylim(0,100)                                 #Set limits of second y axis- adjust to readings you are getting
    #plt2.plot(humidity, 'go-', label='humdity (%)') #plot pressure data
    #plt2.set_ylabel('Humidity (%)')                 #label second y axis
    #plt2.ticklabel_format(useOffset=False)          #Force matplotlib to NOT autoscale y axis
    #plt2.legend(loc='upper right')#plot the legend

    
    
while True: # While loop that loops forever
    while (arduinoData.inWaiting()==0): #Wait here until there is data
        pass #do nothing
    arduinoString = arduinoData.readline() #read the line of text from the serial port
    dataArray = arduinoString.split(',')   #Split it into an array called dataArray
    temp = float( dataArray[0])            #Convert first element to floating number and put in temp
    hum =  float( dataArray[1])            #Convert second element to floating number and put in P
    temperature.append(temp)               #Build our tempF array by appending temp readings
    humidity.append(hum)                   #Building our pressure array by appending P readings
    drawnow(makeFig)                       #Call drawnow to update our live graph
    #drawnow(makeFig2)
    #drawnow(makeFig3)
    plt.pause(.000001)                     #Pause Briefly. Important to keep drawnow from crashing
    cnt=cnt+1
code 2:

import serial # import Serial Library
import numpy  # Import numpy
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *

temperature= []
humidity=[]
arduinoData = serial.Serial('com5', 9600) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0

def makeFig(): #Create a function that makes our desired plot
    plt.ylim(10,30)                                 #Set y min and max values
    plt.title('My Live Streaming Sensor Data')      #Plot the title
    plt.grid(True)                                  #Turn the grid on
    plt.ylabel('Temp')                              #Set ylabels
    plt.plot(temperature, 'ro-', label='Degrees C')        #plot the temperature
    plt.legend(loc='upper left')                    #plot the legend
    #plt2=plt.twinx()                                #Create a second y axis
    #plt.ylim(0,100)                                 #Set limits of second y axis- adjust to readings you are getting
    #plt2.plot(humidity, 'go-', label='humdity (%)') #plot pressure data
    #plt2.set_ylabel('Humidity (%)')                 #label second y axis
    #plt2.ticklabel_format(useOffset=False)          #Force matplotlib to NOT autoscale y axis
    #plt2.legend(loc='upper right')#plot the legend


def makeFig2():
    plt.ylim(0,100)                                 #Set y min and max values
    plt.title('My Live Streaming Sensor Data')      #Plot the title
    plt.grid(True)                                  #Turn the grid on
    plt.ylabel('Humidit')                              #Set ylabels
    plt.plot(humidity, 'ro-', label='(%)')           #plot the temperature
    plt.legend(loc='upper left')                    #plot the legend
    #plt2=plt.twinx()                                #Create a second y axis
    #plt.ylim(0,100)                                 #Set limits of second y axis- adjust to readings you are getting
    #plt2.plot(humidity, 'go-', label='humdity (%)') #plot pressure data
    #plt2.set_ylabel('Humidity (%)')                 #label second y axis
    #plt2.ticklabel_format(useOffset=False)          #Force matplotlib to NOT autoscale y axis
    #plt2.legend(loc='upper right')#plot the legend


#def makeFig3() :
    #plt.ylim(0,100)                                 #Set y min and max values
    #plt.title('My Live Streaming Sensor Data')      #Plot the title
    #plt.grid(True)                                  #Turn the grid on
    #plt.ylabel('Humidit')                              #Set ylabels
    #plt.plot(humidity, 'ro-', label='(%)')           #plot the temperature
    #plt.legend(loc='upper left')                    #plot the legend
    #plt2=plt.twinx()                                #Create a second y axis
    #plt.ylim(0,100)                                 #Set limits of second y axis- adjust to readings you are getting
    #plt2.plot(humidity, 'go-', label='humdity (%)') #plot pressure data
    #plt2.set_ylabel('Humidity (%)')                 #label second y axis
    #plt2.ticklabel_format(useOffset=False)          #Force matplotlib to NOT autoscale y axis
    #plt2.legend(loc='upper right')#plot the legend

    
    
while True: # While loop that loops forever
    while (arduinoData.inWaiting()==0): #Wait here until there is data
        pass #do nothing
    arduinoString = arduinoData.readline() #read the line of text from the serial port
    dataArray = arduinoString.split(',')   #Split it into an array called dataArray
    temp = float( dataArray[0])            #Convert first element to floating number and put in temp
    hum =  float( dataArray[1])            #Convert second element to floating number and put in P
    temperature.append(temp)               #Build our tempF array by appending temp readings
    humidity.append(hum)                   #Building our pressure array by appending P readings
    drawnow(makeFig)                       #Call drawnow to update our live graph
    drawnow(makeFig2)
    #drawnow(makeFig3)
    plt.pause(.000001)                     #Pause Briefly. Important to keep drawnow from crashing
    cnt=cnt+1
Can anyone help me with this problem?

Thanks in advance.