In this tutorial, I will show you how to create Bar graph. Many people use Bar graph because it’s easy to presenting a comparison between 1,2 or 3 value. More than that it’s not recommended because it will be difficult to show the comparison.
Bar Chart can Vertical or Horizontal, depend on what you need.
Single Bar Vertical
Let’s try with Single bar vertical. Type the code below and run in your python text editor.
import matplotlib.pyplot as plt month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] revenue=[100,110,120,100,90,115,70,90,140,100,110,120] plt.bar(month,revenue) plt.title('Simple Bar Graph') plt.xlabel('Month') plt.ylabel('Revenue (K)USD') plt.grid(linestyle='dotted',axis='y') plt.legend() plt.show()
Grid()
You can change the grid style, display only 1 axis (x or y) or display both.
linestyle available: ‘-‘, ‘–‘, ‘-.’, ‘:’, ‘None’, ‘ ‘, ”, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’
axis=’y’ → line from Y
axis=’x’ → line from X
Single Bar Horizontal
You can also visualize the bar by horizontal.
import matplotlib.pyplot as plt month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] revenue=[100,110,120,100,90,115,70,90,140,100,110,120] plt.barh(month,revenue) plt.title('Simple Bar Horizontal') plt.xlabel('Month') plt.ylabel('Revenue (K)USD') plt.grid(linestyle='dotted',axis='x') plt.legend() plt.show()
Combine Bar and Plot
import matplotlib.pyplot as plt month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] revenue=[100,110,120,100,90,115,70,90,140,100,110,120] forcast=[95,100,110,100,95,120,70,95,135,80,110,120] budget=[90,80,110,90,100,120,70,100,130,100,90,100] plt.bar(month,revenue,label='Revenue') plt.plot(month,forcast,marker='o',color='black',label='Forcast') plt.plot(month,budget,marker='o',color='red',label='Budget') plt.title('Simple Bar Graph') plt.xlabel('Month') plt.ylabel('Revenue (K)USD') plt.grid(linestyle='dotted',axis='y') plt.legend() plt.show()
Multiple Bars
If you want to do multiple bar, you can do it by manipulating the thickness and the position of the bar charts.
import matplotlib.pyplot as plt month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] revenue=[100,110,120,100,90,115,70,90,140,100,110,120] forcast=[95,100,110,100,95,120,70,95,135,80,110,120] budget=[90,80,110,90,100,120,70,100,130,100,90,100] plt.bar(month,revenue,label='Revenue',width=0.7) plt.bar(month,forcast,label='Forcast',width=0.4) plt.bar(month,budget,label='Budget',width=0.2) plt.title('Simple Bar Graph') plt.xlabel('Month') plt.ylabel('Revenue (K)USD') plt.grid(linestyle='dotted',axis='y') plt.legend() plt.show()
import matplotlib.pyplot as plt
import numpy as np month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] revenue=[100,110,120,100,90,115,70,90,140,100,110,120] forcast=[95,100,110,100,95,120,70,95,135,80,110,120] budget=[90,80,110,90,100,120,70,100,130,100,90,100] fig=plt.figure() ax=fig.add_axes([0.18,0.15,0.7,0.7]) x=np.array([0,1,2,3,4,5,6,7,8,9,10,11]) ax.bar(x+0.00,revenue,label='Revenue',width=0.25) ax.bar(x+0.25,forcast,label='Forcast',width=0.25) ax.bar(x+0.50,budget,label='Budget',width=0.25) y_pos=[0.2,1.2,2.2,3.2,4.2,5.2,6.2,7.2,8.2,9.2,10.2,11.2] plt.xticks(y_pos,month) plt.title('Simple Bar Graph') plt.xlabel('Month') plt.ylabel('Revenue (K)USD') plt.grid(linestyle='dotted',axis='y') plt.legend() plt.show()
The easy one, you can use python library, pandas.
import matplotlib.pyplot as plt import pandas as pd index=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] actual=[100,110,120,100,90,115,70,90,140,80,110,130] forcast=[95,100,110,100,95,120,70,95,135,80,110,120] budget=[90,80,110,90,100,120,70,100,130,100,90,100] df=pd.DataFrame({'actual':actual,'forcast':forcast,'budget':budget},index=index) ax=df.plot.bar() plt.grid(linestyle='dotted',axis='y') plt.xlabel('Month') plt.ylabel('Revenue (K)USD') plt.title('Bar Graph') plt.show()
Leave a comment