Saturday, February 18, 2017

Bar plot in Python using Matplotlib


import pandas as pd
from collections import Counter
from datetime import datetime, timedelta
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

try:
    data = pd.read_csv('new.csv')

    srn_col = Counter(data['SRN'])
    month_col = Counter(data['Month'])
    scan_col = Counter(data['Scan'])

    scan_dict = {}
    for s in scan_col.keys():
        scan_time_total = datetime.strptime("00:00:00","%H:%M:%S")
        scan_time_total = timedelta(hours=scan_time_total.hour, minutes=scan_time_total.minute, seconds=scan_time_total.second)
        for i,v in data['Scan'].iteritems():
            if s == v:
                temp = data['Time'][i]
                temp = datetime.strptime(data['Time'][i],"%H:%M:%S")
                temp = timedelta(hours=temp.hour, minutes=temp.minute, seconds=temp.second)
                scan_time_total += temp
        #scan_dict[s] = "{0:.0f}".format((scan_time_total.total_seconds())/60)
        #scan_dict[s] = float(scan_time_total.total_seconds()/60)
        scan_dict[s] = (scan_time_total.total_seconds())/60
   
    plt.subplot(211)
    y_pos = np.arange(len(scan_dict.keys()))
    plt.bar(y_pos, scan_dict.values(), align='center', alpha=0.5)
    plt.xticks(y_pos, scan_dict.keys())
    plt.title('Scan vs Time')

    plt.subplot(212)
    y_pos = np.arange(len(scan_col.keys()))
    plt.bar(y_pos, scan_col.values(), align='center', alpha=0.5)
    plt.xticks(y_pos, scan_col.keys())
    plt.title('Scan vs Count')

    plt.show()
   
except Exception as e:
    print(e)