Print continuous line graphs Matplotlib;python

Discussion in 'Programmer's Corner' started by Vindhyachal Takniki, Oct 16, 2015.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    348
    6
    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 (Text):
    1. /*************************main.py*******************************************************/
    2. import time
    3. import graph
    4. hor_pixel = 0
    5. while True:
    6.     if(elapsed_time() >= 10):
    7.            temp_list = read_sensor_data()   #contain 4 values in a list
    8.            graph.print_val(temp_list , hor_pixel)
    9.            hor_pixel = hor_pixel+1;
    10.            if(hor_pixel >= 90):
    11.             hor_pixel = 0
    12.             graph.close();         #close the graph
    13.             graph.init()           #reinit the graph
    14.        
    15.        
    16. except KeyboardInterrupt:
    17.     graph.close();
    18.     GPIO.cleanup()
    19.  
    20.  
    21. graph.close();
    22. GPIO.cleanup()
    23.          
    24.  
    25.  
    26.  
    27. /*************************graph.py*******************************************************/
    28. import matplotlib.pyplot as plt
    29. import time
    30.  
    31. #define global vars, here all vars
    32. #are filled with none, but their type
    33. #will be changed once they are assigned in functions
    34. fig = 'None'
    35. rect = 'None'
    36. ax1 = 'None',
    37. ax2 = 'None',
    38. ax3 = 'None',
    39. ax4 = 'None'
    40. def init():
    41.     global fig,rect,ax1,ax2,ax3,ax4,plt
    42.     fig = plt.figure()
    43.     rect = fig.patch
    44.     rect.set_facecolor('#31312e')
    45.     ax1 = fig.add_subplot(2,2,1, axisbg='grey')
    46.     #ax1.plot(x, y, 'c', linewidth=3.3)
    47.     ax1.set_xlim([0,100])
    48.     ax1.set_ylim([0,100])
    49.     ax1.tick_params(axis='x', colors='c')
    50.     ax1.tick_params(axis='y', colors='c')
    51.     ax1.spines['bottom'].set_color('w')
    52.     ax1.spines['top'].set_color('w')
    53.     ax1.spines['left'].set_color('w')
    54.     ax1.spines['right'].set_color('w')
    55.     ax1.yaxis.label.set_color('c')
    56.     ax1.xaxis.label.set_color('c')
    57.     ax1.set_title('Windspeed', color = 'c')
    58.     ax1.set_xlabel('Read')
    59.     ax1.set_ylabel('Value')
    60.     ax2 = fig.add_subplot(2,2,2, axisbg='grey')
    61.     #ax2.plot(x, y, 'c', linewidth=3.3)
    62.     ax2.set_xlim([0,100])
    63.     ax2.set_ylim([0,100])
    64.     ax2.tick_params(axis='x', colors='c')
    65.     ax2.tick_params(axis='y', colors='c')
    66.     ax2.spines['bottom'].set_color('w')
    67.     ax2.spines['top'].set_color('w')
    68.     ax2.spines['left'].set_color('w')
    69.     ax2.spines['right'].set_color('w')
    70.     ax2.yaxis.label.set_color('c')
    71.     ax2.xaxis.label.set_color('c')
    72.     ax2.set_title('Rainfall', color = 'c')
    73.     ax2.set_xlabel('Read')
    74.     ax2.set_ylabel('Value')
    75.     ax3 = fig.add_subplot(2,2,3, axisbg='grey')
    76.     #ax3.plot(x, y, 'c', linewidth=3.3)
    77.     ax3.set_xlim([0,100])
    78.     ax3.set_ylim([-100,100])
    79.     ax3.tick_params(axis='x', colors='c')
    80.     ax3.tick_params(axis='y', colors='c')
    81.     ax3.spines['bottom'].set_color('w')
    82.     ax3.spines['top'].set_color('w')
    83.     ax3.spines['left'].set_color('w')
    84.     ax3.spines['right'].set_color('w')
    85.     ax3.yaxis.label.set_color('c')
    86.     ax3.xaxis.label.set_color('c')
    87.     ax3.set_title('Temperature', color = 'c')
    88.     ax3.set_xlabel('Read')
    89.     ax3.set_ylabel('C')
    90.     ax4 = fig.add_subplot(2,2,4, axisbg='grey')
    91.     #ax4.plot(x, y, 'c', linewidth=3.3)
    92.     ax4.set_xlim([0,100])
    93.     ax4.set_ylim([0,100])
    94.     ax4.tick_params(axis='x', colors='c')
    95.     ax4.tick_params(axis='y', colors='c')
    96.     ax4.spines['bottom'].set_color('w')
    97.     ax4.spines['top'].set_color('w')
    98.     ax4.spines['left'].set_color('w')
    99.     ax4.spines['right'].set_color('w')
    100.     ax4.yaxis.label.set_color('c')
    101.     ax4.xaxis.label.set_color('c')
    102.     ax4.set_title('Humidity', color = 'c')
    103.     ax4.set_xlabel('Read')
    104.     ax4.set_ylabel('Humidity')
    105.     plt.ion()
    106.     plt.show()
    107.  
    108.  
    109.  
    110. def print_val(temp_list , hor_var):
    111.     global ax1,ax2,ax3,ax4,plt
    112.          
    113.     ax1.scatter(hor_var, temp_list[0])
    114.     ax2.scatter(hor_var, temp_list[1])
    115.     ax3.scatter(hor_var, temp_list[2])
    116.     ax4.scatter(hor_var, temp_list[3])
    117.     plt.draw()
    118.     time.sleep(0.05)
    119.  
    120.  
    121.  
    122. def close():
    123.     global plt
    124.     plt.close()  
     
Loading...