Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python2.7
- import sys
- import os
- import MySQLdb
- import matplotlib
- import matplotlib.dates
- import matplotlib.ticker
- import datetime
- import time
- matplotlib.use('Agg')
- import matplotlib.pyplot as plt
- import matplotlib as mpl
- import numpy as np
- #settings base parametr
- matplotlib.rc('xtick', labelsize=7)
- matplotlib.rc('ytick', labelsize=7)
- matplotlib.rcParams['figure.figsize'] = (12.0, 6.0)
- #connect to db and fetch result
- tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='psswd', db='tempuradb')
- tempura_query = tempura_connect.cursor()
- tempura_query.execute("SELECT * FROM net_storage ORDER BY measure_time DESC LIMIT 61")
- qresult = tempura_query.fetchall()
- m_time, b_in, b_out = zip(*qresult)
- a = 0
- chanel_full_in = []
- chanel_full_out = []
- cf_time = []
- while a <= 59:
- chanel_full_in.append(((((float((b_in[a] - b_in[a+1])*8)/60)/1024)/1024)))
- chanel_full_out.append(((((float((b_out[a] - b_out[a+1])*8)/60)/1024)/1024)))
- cf_time.append(m_time[a+1])
- a += 1
- figure = plt.figure()
- axes = figure.add_subplot (1, 1, 1)
- max_yaxis_value = max(max(chanel_full_in), max(chanel_full_out))
- min_yaxis_value = min(min(chanel_full_in), min(chanel_full_out))
- #format grid
- axes.set_ylabel('Mbit per second', fontsize=8)
- axes.xaxis.grid(True, which='major', linestyle='solid')
- axes.xaxis.grid(True, which='minor')
- axes.yaxis.grid(True, which='major')
- axes.axis([min(cf_time), max(cf_time), min_yaxis_value, max_yaxis_value + 0.1])
- 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])
- 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])
- y_step = matplotlib.ticker.MultipleLocator(base = 0.5)
- axes.yaxis.set_major_locator(y_step)
- f_min = mpl.dates.DateFormatter('%M')
- f_day = mpl.dates.DateFormatter('%H')
- axes.xaxis.set_major_formatter(f_day)
- day_tick = mpl.dates.HourLocator()
- axes.xaxis.set_major_locator(day_tick)
- major_labels = [tick.label1 for tick in axes.xaxis.get_major_ticks()]
- for label in major_labels:
- label.set_rotation(90)
- label.set_ha('center')
- label.set_va('bottom')
- label.set_bbox(dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.5'))
- axes.xaxis.set_tick_params(which='major', pad=-10)
- axes.xaxis.set_minor_formatter(f_min)
- min_tick = mpl.dates.MinuteLocator(interval=1)
- axes.xaxis.set_minor_locator(min_tick)
- minor_labels = [tick.label1 for tick in axes.xaxis.get_minor_ticks()]
- for label in minor_labels:
- # label.set_rotation(35)
- label.set_ha('center')
- plt.title('Traffic in the 1 hour', fontsize=10)
- axes.legend(loc='upper right', shadow=True, fontsize='small')
- plt.savefig('/var/www/htdocs/wordpress/tempura/traffic_chart/1hournet_graph.png', format='png')
- tempura_query.close()
- tempura_connect.close()
Advertisement
Add Comment
Please, Sign In to add comment