Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, matplotlib.pyplot as plt
- import matplotlib.dates as md
- import datetime as dt
- import time
- class Chart:
- def __init__(self):
- init_chart(self)
- self.last_data = []
- now=time.mktime(time.localtime())
- date= dt.datetime.fromtimestamp(now)
- datenum = md.date2num(date)
- self.last_point = datenum
- def update(self, *args, **kw):
- update_chart(self, *args, **kw)
- def init_chart(C):
- import pylab as p
- p.ion()
- figure, (ax0, ax1, ax2, ax3, ax4, ax5) = p.subplots(nrows=6)
- figure.tight_layout()
- # C.ax = (ax0, ax1, ax2, ax3, ax4, ax5)
- #figure.set_figsize_inches(15, 20)
- figure.canvas.set_window_title('Chart for LVE 507')
- p.subplots_adjust(bottom=0.17, hspace=0.2, wspace = 0.3)
- p.rcParams['ytick.major.size'] = 3
- # cpu
- ax0 = p.subplot(6, 1, 1)
- ax0.get_xaxis().set_visible(False)
- p.title('CPU Usage')
- p.grid('on')
- arr = (0, 25, 50, 75, 100)
- p.yticks(arr, ['%d%%' % t for t in arr] )
- p.ylim(0, 100)
- # ram
- ax1 = p.subplot(6, 1, 2)
- ax1.get_xaxis().set_visible(False)
- p.title('Physical Memory Usage')
- arr = (0, 256, 512, 768, 1024)
- p.yticks(arr, ['0B']+['%d MB' % t for t in arr[1:-1]]+['1G'] )
- p.ylim(0, 1024)
- p.grid('on')
- # input/output
- ax2 = p.subplot(6, 1, 3)
- ax2.get_xaxis().set_visible(False)
- p.title('Input/Output Usage')
- arr = (0, 256, 512, 768, 1024)
- p.yticks(arr, ['%dKB/s' % t for t in arr[:-1]]+['1MB/s'] )
- p.ylim(0, 1024)
- p.grid('on')
- #Entry Processes cpu
- ax3 = p.subplot(6, 1, 4)
- ax3.get_xaxis().set_visible(False)
- p.title('Entry Processes')
- p.grid('on')
- # Processes
- ax4 = p.subplot(6, 1, 5)
- ax4.get_xaxis().set_visible(False)
- p.title('Processes')
- p.grid('on')
- # Faults
- p.subplot(6, 1, 6)
- p.title('Faults')
- p.grid('on')
- p.draw()
- def update_chart(C, datenum, *args, **kw):
- # args must be Python lists of cpu, mem etc units
- L = len(args)
- assert L == 6
- import pylab as p
- if len(C.last_data):
- for i, arg in enumerate(args):
- Arg = [C.last_data[i]]+arg
- p.subplot(L, 1, i+1)
- if i < 3:
- p.plot((C.last_point, datenum), Arg, 'b')
- else:
- p.bar((C.last_point, datenum), Arg, 0.000002, color='b')
- p.draw()
- C.last_data = [arg[-1] for arg in args]
- C.last_point = datenum
- #C.last_point += l
- if __name__ == '__main__':
- c = Chart()
- from math import sin, cos
- for i in range(5):
- now=time.mktime(time.localtime())
- date= dt.datetime.fromtimestamp(now)
- datenum = md.date2num(date)
- plt.xticks(rotation=90)
- ax=plt.gca()
- xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
- ax.xaxis.set_major_formatter(xfmt)
- c.update(datenum, [10+sin(i)], [500+100*cos(i)], [500+100*cos(3*i+0.4)],
- [15+int(10*cos(2*i))], [20+int(10*cos(4*i))], [5+int(2*cos(7*i))])
- os.system('sleep 1')
- import pylab as p
- p.ioff()
- p.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement