Python: How to plot grouped bar graph?

The script:

(SAVE this below script with ".py" or ".ipynb" extensions)

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

import numpy as np

sns.set()

departure=pd.read_csv('file.csv')

departure

df = pd.DataFrame(departure, columns = ['year', 'var1', 'var2', 'var3'])

print(df)

plt.figure(figsize=(9,6))

pos = list(range(len(df['var1'])))

width = 0.25

N=35

ind = np.arange(N)

fig, ax = plt.subplots(figsize=(40,15))

plt.bar(pos, df['var1'], width, alpha=0.5, color='#059C1E')

plt.bar([p + width for p in pos], df['var2'], width, alpha=0.5, color='#1032E0')

plt.bar([p + width*2 for p in pos], df['var3'], width, alpha=0.5, color='#000000')

ax.set_ylabel('Y-AXIS',fontweight='bold')

plt.xlabel('Year', fontweight='bold')

ax.set_title('Title')

plt.xlim(min(pos)-width, max(pos)+width*4)

plt.ylim([-42, max(df['var1'] + df['var2'] + df['var3'])])

plt.xticks(ind+width/2., ('1982','1983','1984','1985','1986','1987','1988','1989','1990','1991','1992','1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'))

plt.legend(['legend1', 'legend2', 'legend3'], loc='upper right')

plt.xticks(np.arange(1982, 2016, 1))

plt.grid(True, color = "grey")

plt.grid(axis = 'y', linestyle = '-')

plt.savefig("output.png") 

plt.ylim((-17,42))

plt.show()

Comments

Popular posts from this blog

ഔ ന്റെ കുദരേ അൻക്ക് പിരാന്താണ്!!

GrADS: Some useful prerequisite scripts - basemap.gs

ഇൻഡോർ: ഞാൻ താമസിച്ച ആദ്യ ഉത്തരേന്ത്യൻ സിറ്റിയും ഒരു എൻ.സി.സി. നാഷണൽ ക്യാമ്പും