Sunday, July 30, 2017

Python Matplotlib and Barchart


import numpy as np import pandas as pd import matplotlib.pyplot as plt #Importing Data Download Data here... df = pd.read_csv(r'F:\Python\TimeSeries\shampoo_sales.txt', sep='\t', header=0) df['Month'] = pd.to_datetime(df['Month']) #Defining Function for data points def autolabel(rects, bottom=0, fontsize=10): """ Attach a text label above each bar displaying its height """ i=0 for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2., height/2+bottom[i], '%d' % int(height), horizontalalignment ='center', verticalalignment ='bottom', fontsize=fontsize, rotation=90, color='blue') i+=1 fig, ax = plt.subplots(figsize=(10,7), dpi=100, frameon=True) plt.subplots_adjust(left=0, bottom=0, right=1, top=0.8, wspace=0, hspace=0) #ax.set_ylim([0, 725]) ax.spines['right'].set_visible(False) ax.spines['left'].set_visible(False) ax.spines['top'].set_visible(False) #ax.spines['bottom'].set_visible(False) ax.yaxis.grid(b=False, which='major', color='gray', linestyle='--') bottom_01 = [0]*len(df) bar1 = ax.bar(np.arange(len(df)), df['Shampoo_Sales'], width=0.8, color='#CBFE34', alpha=0.8, bottom=bottom_01) bottom_02 = bottom_01 + df['Shampoo_Sales'] bar2 = ax.bar(np.arange(len(df)), df['Other_Sales'], width=0.8, color='red', alpha=0.8, bottom=bottom_02) line = ax.plot(np.arange(len(df)), df['Shampoo_Sales']+df['Other_Sales'], marker='o', linestyle='-', color='#5291A2') ax.set_xticks(np.arange(len(df))) ax.set_xticklabels(labels=[str(dt)[0:10] for dt in df['Month']], minor=False, rotation=90) autolabel(rects=bar1, bottom=bottom_01, fontsize=10) autolabel(rects=bar2, bottom=bottom_02, fontsize=10) plt.show()



No comments:

Post a Comment