Print continuous 4 line graphs Matplotlib

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

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    1. I have to print values of 4 different sensor taken at every 10 sec in line graph.
    So I have used subplots to do so.
    I took reading every 10 sec, plot them for 100 such readings & then close the graph & start over again.
    This is continuous graph, i.e every 10 sec I read value, I have to print it & so on for 15 minutes.
    It is not like I have values in array/list before printing the graph.

    2. I had took reference from below links:
    http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop-with-matplotlib
    http://pythonprogramming.net/matplotlib-tutorial-part-5-subplots-multiple-plots-figure/
    http://stackoverflow.com/questions/8213522/matplotlib-clearing-a-plot-when-to-use-cla-clf-or-close


    3. I am able to do so by scatter plot. My code is below. It has two files: main.py & graph.py.
    main.py calls graph.py.

    4. I am using python 2.7 & today I have downloaded matplotlib.
    Attached is code file. It has two files in it.
    Also code link is: http://www.docdroid.net/z5Ba1IS/graph.txt.html


    problems:
    1. In graph.py, I have function print_val(). In this I have called ax1.scatter() to print scatter graph.
    I need to print line graph. if I replace it with ax1.plot(x,y), it don't print anything.
    i have to do it by line graph. how to do that.

    2. In graph.py functions, I have defined global variables as "global fig,rect,ax1,ax2,ax3,ax4,plt"
    But they are not defined globally in file, but inside the functions.
    I don't know somehow code is working.
    Can someone suggest me the correct way to do that so that ax1 defined in init(), is same as in print_val().
    I think classes would be correct option for this. But I have no experience of it.


    3. in main.py, I have defined "except KeyboardInterrupt: graph.close(); GPIO.cleanup() ".
    But when I exit the code by pressing ctrl+z , graph screen remains on screen.

    4. While graph is plotting, if I minimize the graph screen & then maximize it.
    Graph disappears from the open window & only grey screen appears.

    Code (Text):
    1. /*************************main.py*******************************************************/
    2.  
    3. import time
    4. import graph
    5.  
    6. hor_pixel = 0
    7.  
    8. while True:
    9.     if(elapsed_time() >= 10):
    10.            temp_list = read_sensor_data()   #contain 4 values in a list
    11.            graph.print_val(temp_list , hor_pixel)
    12.            hor_pixel = hor_pixel+1;
    13.            if(hor_pixel >= 100):
    14.             hor_pixel = 0
    15.             graph.close();
    16.             graph.init()
    17.          
    18.          
    19. except KeyboardInterrupt:
    20.     graph.close();
    21.     GPIO.cleanup()
    22.    
    23.    
    24. graph.close();
    25. GPIO.cleanup()  
    26.            
    27.  
    28. /*************************graph.py*******************************************************/
    29.  
    30.  
    31. import matplotlib.pyplot as plt
    32. import time
    33.  
    34.  
    35. def init():
    36.     global fig,rect,ax1,ax2,ax3,ax4,plt
    37.  
    38.     fig = plt.figure()
    39.     rect = fig.patch
    40.     rect.set_facecolor('#31312e')
    41.     ax1 = fig.add_subplot(2,2,1, axisbg='grey')
    42.     #ax1.plot(x, y, 'c', linewidth=3.3)
    43.     ax1.set_xlim([0,100])
    44.     ax1.set_ylim([0,100])
    45.     ax1.tick_params(axis='x', colors='c')
    46.     ax1.tick_params(axis='y', colors='c')
    47.     ax1.spines['bottom'].set_color('w')
    48.     ax1.spines['top'].set_color('w')
    49.     ax1.spines['left'].set_color('w')
    50.     ax1.spines['right'].set_color('w')
    51.     ax1.yaxis.label.set_color('c')
    52.     ax1.xaxis.label.set_color('c')
    53.     ax1.set_title('Windspeed', color = 'c')
    54.     ax1.set_xlabel('Read')
    55.     ax1.set_ylabel('Value')
    56.  
    57.     ax2 = fig.add_subplot(2,2,2, axisbg='grey')
    58.     #ax2.plot(x, y, 'c', linewidth=3.3)
    59.     ax2.set_xlim([0,100])
    60.     ax2.set_ylim([0,100])
    61.     ax2.tick_params(axis='x', colors='c')
    62.     ax2.tick_params(axis='y', colors='c')
    63.     ax2.spines['bottom'].set_color('w')
    64.     ax2.spines['top'].set_color('w')
    65.     ax2.spines['left'].set_color('w')
    66.     ax2.spines['right'].set_color('w')
    67.     ax2.yaxis.label.set_color('c')
    68.     ax2.xaxis.label.set_color('c')
    69.     ax2.set_title('Rainfall', color = 'c')
    70.     ax2.set_xlabel('Read')
    71.     ax2.set_ylabel('Value')
    72.  
    73.     ax3 = fig.add_subplot(2,2,3, axisbg='grey')
    74.     #ax3.plot(x, y, 'c', linewidth=3.3)
    75.     ax3.set_xlim([0,100])
    76.     ax3.set_ylim([-100,100])
    77.     ax3.tick_params(axis='x', colors='c')
    78.     ax3.tick_params(axis='y', colors='c')
    79.     ax3.spines['bottom'].set_color('w')
    80.     ax3.spines['top'].set_color('w')
    81.     ax3.spines['left'].set_color('w')
    82.     ax3.spines['right'].set_color('w')
    83.     ax3.yaxis.label.set_color('c')
    84.     ax3.xaxis.label.set_color('c')
    85.     ax3.set_title('Temperature', color = 'c')
    86.     ax3.set_xlabel('Read')
    87.     ax3.set_ylabel('C')
    88.  
    89.  
    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.  
    106.     plt.ion()
    107.     plt.show()
    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. def close():
    122.     global plt
    123.     plt.close()  
    124.    
     
Loading...