Sunday, July 30, 2017

Python Matplotlib and Line Chart with Data Point


import numpy as np import pandas as pd import matplotlib.pyplot as plt import random #Importing Data df = pd.read_csv(r'F:\Python\...\shampoo_sales.txt', sep='\t', header=0) df['Month'] = pd.to_datetime(df['Month']) df.sort_values(['Month'], ascending=[True], inplace=True) fig, ax = plt.subplots(figsize=(10,7), dpi=70, frameon=True) plt.subplots_adjust(left=0, bottom=0, right=1, top=0.8, wspace=0, hspace=0) 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='-.') ax.xaxis.grid(b=False, which='major', color='gray', linestyle='-.') line = ax.plot(np.arange(len(df)), df['Shampoo_Sales'], marker='o', linestyle='-', linewidth=5, color='#0F9F5B') #ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Time - Month') ax.set_title('Shampoo Sales Over Time') ax.set_xticks(np.arange(len(df))) ax.set_xticklabels(labels=[str(dt)[0:7] for dt in df['Month']], minor=False, rotation=90) plt.show() """ ================ =============================== character description ================ =============================== - solid line style -- dashed line style -. dash-dot line style : dotted line style . point marker , pixel marker o circle marker v triangle_down marker ^ triangle_up marker < triangle_left marker > triangle_right marker 1 tri_down marker 2 tri_up marker 3 tri_left marker 4 tri_right marker s square marker p pentagon marker * star marker h hexagon1 marker H hexagon2 marker + plus marker x x marker D diamond marker d thin_diamond marker | vline marker _ hline marker ================ =============================== """



To add data point automatically on the line chart use the below code.
import numpy as np import pandas as pd import matplotlib.pyplot as plt import random #Importing Data df = pd.read_csv(r'F:\Python\...\shampoo_sales.txt', sep='\t', header=0) df['Month'] = pd.to_datetime(df['Month']) df.sort_values(['Month'], ascending=[True], inplace=True) #Defining Function for data points def autolabel(rects, fontsize=10): """ Attach a text label above each bar displaying its height """ for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2., height-0, '%d' % int(height), horizontalalignment ='center', verticalalignment ='bottom', fontsize=fontsize, rotation=45, color='blue') fig, ax = plt.subplots(figsize=(10,7), dpi=70, frameon=True) plt.subplots_adjust(left=0, bottom=0, right=1, top=0.8, wspace=0, hspace=0) 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='-.') ax.xaxis.grid(b=False, which='major', color='gray', linestyle='-.') bottom_01 = [0]*len(df) bar = ax.bar(np.arange(len(df)), df['Shampoo_Sales'], width=0.8, color='w', alpha=0.8, bottom=bottom_01) line = ax.plot(np.arange(len(df)), df['Shampoo_Sales'], marker='o', linestyle='-', linewidth=5, color='#0F9F5B') #ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Time - Month') ax.set_title('Shampoo Sales Over Time') ax.set_xticks(np.arange(len(df))) ax.set_xticklabels(labels=[str(dt)[0:7] for dt in df['Month']], minor=False, rotation=90) autolabel(rects=bar, fontsize=8) plt.show()





No comments:

Post a Comment