Advertisement
Dmitrey15

lve

Mar 5th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import os, matplotlib.pyplot as plt
  2. import matplotlib.dates as md
  3. import datetime as dt
  4. import time
  5.  
  6. class Chart:
  7.     def __init__(self):
  8.         init_chart(self)
  9.         self.last_data = []
  10.         now=time.mktime(time.localtime())
  11.         date= dt.datetime.fromtimestamp(now)
  12.         datenum = md.date2num(date)
  13.         self.last_point = datenum
  14.     def update(self, *args, **kw):
  15.         update_chart(self, *args, **kw)
  16.  
  17.  
  18. def init_chart(C):
  19.     import pylab as p
  20.     p.ion()
  21.     figure, (ax0, ax1, ax2, ax3, ax4, ax5) = p.subplots(nrows=6)
  22.     figure.tight_layout()
  23. #    C.ax = (ax0, ax1, ax2, ax3, ax4, ax5)
  24.  
  25.     #figure.set_figsize_inches(15, 20)
  26.     figure.canvas.set_window_title('Chart for LVE 507')
  27.    
  28.     p.subplots_adjust(bottom=0.17, hspace=0.2, wspace = 0.3)
  29.     p.rcParams['ytick.major.size'] = 3
  30.    
  31.    
  32.     # cpu
  33.     ax0 = p.subplot(6, 1, 1)
  34.     ax0.get_xaxis().set_visible(False)
  35.     p.title('CPU Usage')
  36.     p.grid('on')
  37.     arr = (0, 25, 50, 75, 100)
  38.     p.yticks(arr, ['%d%%' % t for t in arr] )
  39.     p.ylim(0, 100)
  40.    
  41.     # ram
  42.     ax1 = p.subplot(6, 1, 2)
  43.     ax1.get_xaxis().set_visible(False)
  44.     p.title('Physical Memory Usage')
  45.     arr = (0, 256, 512, 768, 1024)
  46.     p.yticks(arr, ['0B']+['%d MB' % t for t in arr[1:-1]]+['1G'] )
  47.     p.ylim(0, 1024)
  48.     p.grid('on')
  49.    
  50.     # input/output
  51.     ax2 = p.subplot(6, 1, 3)
  52.     ax2.get_xaxis().set_visible(False)
  53.     p.title('Input/Output Usage')
  54.     arr = (0, 256, 512, 768, 1024)
  55.     p.yticks(arr, ['%dKB/s' % t for t in arr[:-1]]+['1MB/s'] )
  56.     p.ylim(0, 1024)
  57.     p.grid('on')
  58.    
  59.     #Entry Processes cpu
  60.     ax3 = p.subplot(6, 1, 4)
  61.     ax3.get_xaxis().set_visible(False)
  62.     p.title('Entry Processes')
  63.     p.grid('on')
  64.    
  65.     # Processes
  66.     ax4 = p.subplot(6, 1, 5)
  67.     ax4.get_xaxis().set_visible(False)
  68.     p.title('Processes')
  69.     p.grid('on')
  70.    
  71.     # Faults
  72.     p.subplot(6, 1, 6)
  73.     p.title('Faults')
  74.     p.grid('on')
  75.    
  76.     p.draw()
  77.  
  78.  
  79. def update_chart(C, datenum, *args, **kw):
  80.     # args must be Python lists of cpu, mem etc units
  81.     L = len(args)
  82.     assert L == 6
  83.     import pylab as p
  84.    
  85.     if len(C.last_data):
  86.         for i, arg in enumerate(args):
  87.             Arg = [C.last_data[i]]+arg
  88.             p.subplot(L, 1, i+1)
  89.             if i < 3:
  90.                 p.plot((C.last_point, datenum), Arg, 'b')
  91.             else:
  92.                 p.bar((C.last_point, datenum), Arg, 0.000002, color='b')
  93.         p.draw()
  94.  
  95.     C.last_data = [arg[-1] for arg in args]
  96.    
  97.     C.last_point = datenum
  98.     #C.last_point += l
  99.  
  100. if __name__ == '__main__':
  101.  
  102.     c = Chart()
  103.     from math import sin, cos
  104.     for i in range(5):
  105.         now=time.mktime(time.localtime())
  106.         date= dt.datetime.fromtimestamp(now)
  107.         datenum = md.date2num(date)
  108.         plt.xticks(rotation=90)
  109.         ax=plt.gca()
  110.         xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
  111.         ax.xaxis.set_major_formatter(xfmt)
  112.         c.update(datenum, [10+sin(i)], [500+100*cos(i)], [500+100*cos(3*i+0.4)],
  113.                  [15+int(10*cos(2*i))], [20+int(10*cos(4*i))], [5+int(2*cos(7*i))])
  114.         os.system('sleep 1')
  115.     import pylab as p
  116.     p.ioff()
  117.     p.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement