Print continuous 4 line graphs Matplotlib - Page 1
Forum section: electronics / beginners

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: October 15, 2015, 11:39:17 am
content: 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-matplotlibhttp://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-close3. 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. Code is here also:http://www.docdroid.net/z5Ba1IS/graph.txt.htmlproblems: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:/*************************main.py*******************************************************/import timeimport graphhor_pixel = 0while True:if(elapsed_time() >= 10):temp_list = read_sensor_data() #contain 4 values in a listgraph.print_val(temp_list , hor_pixel)hor_pixel = hor_pixel+1;if(hor_pixel >= 100):hor_pixel = 0graph.close();graph.init()except KeyboardInterrupt:graph.close();GPIO.cleanup()graph.close();GPIO.cleanup()/*************************graph.py*******************************************************/import matplotlib.pyplot as pltimport timedef init():global fig,rect,ax1,ax2,ax3,ax4,pltfig = plt.figure()rect = fig.patchrect.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,pltax1.scatter(hor_var, temp_list)ax2.scatter(hor_var, temp_list)ax3.scatter(hor_var, temp_list)ax4.scatter(hor_var, temp_list)plt.draw()time.sleep(0.05)def close():global pltplt.close()