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

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: October 16, 2015, 10:39:27 am
content: This is in part problem of link here:https://www.eevblog.com/forum/beginners/print-continuous-4-line-graphs-matplotlib/msg778164/#msg7781641. 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 lines3. 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.pyCode:/*************************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 >= 90):hor_pixel = 0graph.close(); #close the graphgraph.init() #reinit the graphexcept KeyboardInterrupt:graph.close();GPIO.cleanup()graph.close();GPIO.cleanup()/*************************graph.py*******************************************************/import matplotlib.pyplot as pltimport time#define global vars, here all vars#are filled with none, but their type#will be changed once they are assigned in functionsfig = 'None'rect = 'None'ax1 = 'None',ax2 = 'None',ax3 = 'None',ax4 = 'None'def 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()