Print continuous line graphs Matplotlib;python

Thread Starter

Vindhyachal Takniki

Joined Nov 3, 2014
471
This is in part problem of link here:
http://forum.allaboutcircuits.com/threads/print-continuous-4-line-graphs-matplotlib.116549/

1. In that thread I had multiple problem. In this thread I am breaking down into simple one:
To print continuous 4 line graphs in subplot.

2. My current code works for scatter plot. It prints scateer values correctly. But I want to replace it with lines

3. This is continuous graph. I read values from sensor every 10 sec, print them on graph for 15 minutes. then delete the graph. Then start over again.

4. Below is my code. it has two files: main.py & graph.py


Code:
/*************************main.py*******************************************************/
import time
import graph
hor_pixel = 0
while True:
    if(elapsed_time() >= 10):
           temp_list = read_sensor_data()   #contain 4 values in a list
           graph.print_val(temp_list , hor_pixel)
           hor_pixel = hor_pixel+1;
           if(hor_pixel >= 90):
            hor_pixel = 0
            graph.close();         #close the graph
            graph.init()           #reinit the graph
       
       
except KeyboardInterrupt:
    graph.close();
    GPIO.cleanup()
 
 
graph.close();
GPIO.cleanup()
         



/*************************graph.py*******************************************************/
import matplotlib.pyplot as plt
import time

#define global vars, here all vars
#are filled with none, but their type
#will be changed once they are assigned in functions
fig = 'None'
rect = 'None'
ax1 = 'None',
ax2 = 'None',
ax3 = 'None',
ax4 = 'None'
def init():
    global fig,rect,ax1,ax2,ax3,ax4,plt
    fig = plt.figure()
    rect = fig.patch
    rect.set_facecolor('#31312e')
    ax1 = fig.add_subplot(2,2,1, axisbg='grey')
    #ax1.plot(x, y, 'c', linewidth=3.3)
    ax1.set_xlim([0,100])
    ax1.set_ylim([0,100])
    ax1.tick_params(axis='x', colors='c')
    ax1.tick_params(axis='y', colors='c')
    ax1.spines['bottom'].set_color('w')
    ax1.spines['top'].set_color('w')
    ax1.spines['left'].set_color('w')
    ax1.spines['right'].set_color('w')
    ax1.yaxis.label.set_color('c')
    ax1.xaxis.label.set_color('c')
    ax1.set_title('Windspeed', color = 'c')
    ax1.set_xlabel('Read')
    ax1.set_ylabel('Value')
    ax2 = fig.add_subplot(2,2,2, axisbg='grey')
    #ax2.plot(x, y, 'c', linewidth=3.3)
    ax2.set_xlim([0,100])
    ax2.set_ylim([0,100])
    ax2.tick_params(axis='x', colors='c')
    ax2.tick_params(axis='y', colors='c')
    ax2.spines['bottom'].set_color('w')
    ax2.spines['top'].set_color('w')
    ax2.spines['left'].set_color('w')
    ax2.spines['right'].set_color('w')
    ax2.yaxis.label.set_color('c')
    ax2.xaxis.label.set_color('c')
    ax2.set_title('Rainfall', color = 'c')
    ax2.set_xlabel('Read')
    ax2.set_ylabel('Value')
    ax3 = fig.add_subplot(2,2,3, axisbg='grey')
    #ax3.plot(x, y, 'c', linewidth=3.3)
    ax3.set_xlim([0,100])
    ax3.set_ylim([-100,100])
    ax3.tick_params(axis='x', colors='c')
    ax3.tick_params(axis='y', colors='c')
    ax3.spines['bottom'].set_color('w')
    ax3.spines['top'].set_color('w')
    ax3.spines['left'].set_color('w')
    ax3.spines['right'].set_color('w')
    ax3.yaxis.label.set_color('c')
    ax3.xaxis.label.set_color('c')
    ax3.set_title('Temperature', color = 'c')
    ax3.set_xlabel('Read')
    ax3.set_ylabel('C')
    ax4 = fig.add_subplot(2,2,4, axisbg='grey')
    #ax4.plot(x, y, 'c', linewidth=3.3)
    ax4.set_xlim([0,100])
    ax4.set_ylim([0,100])
    ax4.tick_params(axis='x', colors='c')
    ax4.tick_params(axis='y', colors='c')
    ax4.spines['bottom'].set_color('w')
    ax4.spines['top'].set_color('w')
    ax4.spines['left'].set_color('w')
    ax4.spines['right'].set_color('w')
    ax4.yaxis.label.set_color('c')
    ax4.xaxis.label.set_color('c')
    ax4.set_title('Humidity', color = 'c')
    ax4.set_xlabel('Read')
    ax4.set_ylabel('Humidity')
    plt.ion()
    plt.show()



def print_val(temp_list , hor_var):
    global ax1,ax2,ax3,ax4,plt
         
    ax1.scatter(hor_var, temp_list[0])
    ax2.scatter(hor_var, temp_list[1])
    ax3.scatter(hor_var, temp_list[2])
    ax4.scatter(hor_var, temp_list[3])
    plt.draw()
    time.sleep(0.05)
 


def close():
    global plt
    plt.close()
 
Top