autumnrd

Traffic bar chart from db and datetime on x axis

Nov 28th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. #!/usr/local/bin/python2.7
  2.  
  3. import sys
  4. import os
  5. import MySQLdb
  6. import matplotlib
  7. import matplotlib.dates
  8. import matplotlib.ticker
  9. import datetime
  10. import time
  11. matplotlib.use('Agg')
  12.  
  13. import matplotlib.pyplot as plt
  14. import matplotlib as mpl
  15. import numpy as np
  16.  
  17. #settings base parametr
  18. matplotlib.rc('xtick', labelsize=7)
  19. matplotlib.rc('ytick', labelsize=7)
  20. matplotlib.rcParams['figure.figsize'] = (12.0, 6.0)
  21.  
  22. #connect to db and fetch result
  23. tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='psswd', db='tempuradb')
  24. tempura_query = tempura_connect.cursor()
  25. tempura_query.execute("SELECT * FROM net_storage ORDER BY measure_time DESC LIMIT 61")
  26. qresult = tempura_query.fetchall()
  27. m_time, b_in, b_out = zip(*qresult)
  28.  
  29. a = 0
  30. chanel_full_in = []
  31. chanel_full_out = []
  32. cf_time = []
  33.  
  34. while a <= 59:
  35.     chanel_full_in.append(((((float((b_in[a] - b_in[a+1])*8)/60)/1024)/1024)))
  36.     chanel_full_out.append(((((float((b_out[a] - b_out[a+1])*8)/60)/1024)/1024)))
  37.     cf_time.append(m_time[a+1])
  38.     a += 1
  39.  
  40. figure = plt.figure()
  41. axes = figure.add_subplot (1, 1, 1)
  42.  
  43. max_yaxis_value = max(max(chanel_full_in), max(chanel_full_out))
  44. min_yaxis_value = min(min(chanel_full_in), min(chanel_full_out))
  45.  
  46. #format grid
  47. axes.set_ylabel('Mbit per second', fontsize=8)
  48. axes.xaxis.grid(True, which='major', linestyle='solid')
  49. axes.xaxis.grid(True, which='minor')
  50. axes.yaxis.grid(True, which='major')
  51. axes.axis([min(cf_time), max(cf_time), min_yaxis_value, max_yaxis_value + 0.1])
  52.  
  53. axes.bar(cf_time, chanel_full_out, 0.0007, 0.05, color='green', edgecolor='green', alpha=0.5, label = 'max out : '+str(max(chanel_full_out))[:-9])
  54. axes.bar(cf_time, chanel_full_in, 0.0007, 0.05, color='orange', edgecolor='orange', alpha=0.6, label = 'max in : '+str(max(chanel_full_in))[:-9])
  55.  
  56. y_step = matplotlib.ticker.MultipleLocator(base = 0.5)
  57. axes.yaxis.set_major_locator(y_step)
  58.  
  59. f_min = mpl.dates.DateFormatter('%M')
  60. f_day = mpl.dates.DateFormatter('%H')
  61.  
  62. axes.xaxis.set_major_formatter(f_day)
  63. day_tick = mpl.dates.HourLocator()
  64. axes.xaxis.set_major_locator(day_tick)
  65. major_labels = [tick.label1 for tick in axes.xaxis.get_major_ticks()]
  66. for label in major_labels:
  67.     label.set_rotation(90)
  68.     label.set_ha('center')
  69.     label.set_va('bottom')
  70.     label.set_bbox(dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.5'))
  71. axes.xaxis.set_tick_params(which='major', pad=-10)
  72.  
  73. axes.xaxis.set_minor_formatter(f_min)
  74. min_tick = mpl.dates.MinuteLocator(interval=1)
  75. axes.xaxis.set_minor_locator(min_tick)
  76. minor_labels = [tick.label1 for tick in axes.xaxis.get_minor_ticks()]
  77. for label in minor_labels:
  78. #    label.set_rotation(35)
  79.     label.set_ha('center')
  80.  
  81. plt.title('Traffic in the 1 hour', fontsize=10)
  82. axes.legend(loc='upper right', shadow=True, fontsize='small')
  83.  
  84. plt.savefig('/var/www/htdocs/wordpress/tempura/traffic_chart/1hournet_graph.png', format='png')
  85.  
  86. tempura_query.close()
  87. tempura_connect.close()
Advertisement
Add Comment
Please, Sign In to add comment