vindhyachal
Newbie level 4
1. Need 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
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 Python - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /*************************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()
Code Python - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 /*************************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()