Bar plots in Matplotlib – Data Visualization using Python

Matplotlib can be used to draw simple as well as complex bar plots. In this article we will see how to draw bar plots along with available options.

Here is an example of simple bar plot with available options.

################################################################################################
# name: barplot-03.py
# desc: Simple bar plot with options
# date: 2018-07-02
# Author: conquistadorjd
# Documentation : https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html#matplotlib.pyplot.bar
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales = [71, 91, 56, 66, 52, 27]
salesyerr = [5, 5, 5, 5, 5, 5]
# This is with mandatory fields
# plt.bar(np.arange(len(drinks)), sales)
# This is with multiple options
plt.bar(np.arange(len(drinks)), sales, width=0.5, yerr=salesyerr, label="Jagur", color='r',edgecolor='b', fill=True, hatch='*',linestyle='–',align='center')
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is bar plot using matplotlib')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-03.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
barplot-03.py
hosted with ❤ by GitHub

Output of this script

Horizontal Bar plot

################################################################################################
# name: barplot-04.py
# desc: Simple bar plot with options – horizontal bar
# date: 2018-07-02
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales = [71, 91, 56, 66, 52, 27]
plt.barh(np.arange(len(drinks)), sales,label="Jagur", color='b',edgecolor='b')
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is bar plot using matplotlib')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-04.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
barplot-04.py
hosted with ❤ by GitHub

Output

Stacked bar plot

################################################################################################
# name: barplot-05.py
# desc: stacked bar plot
# date: 2018-07-02
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales = [71, 91, 56, 66, 52, 27]
inquiry = [60, 50, 70, 66, 52, 27]
salesyerr = [5, 5, 5, 5, 5, 5]
inquiryerr = [5, 5, 5, 5, 5, 5]
plt.bar(np.arange(len(drinks)), sales,width=0.5,label="Jagur", color='r',edgecolor='r',yerr=salesyerr)
plt.bar(np.arange(len(drinks)), inquiry,bottom=sales,width=0.5,label="Range Rover", color='b',edgecolor='b',yerr=inquiryerr)
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is bar plot using matplotlib')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-05.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
barplot-05.py
hosted with ❤ by GitHub

Output

bar plot with multiple data series

################################################################################################
# name: barplot-06.py
# desc: stacked bar plot
# date: 2018-07-02
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
jagur = [71, 91, 56, 66, 52, 95]
rangerover = [60, 50, 70, 85, 69, 27]
jaguryerr = [5, 5, 5, 5, 5, 5]
rangerovererr = [5, 5, 5, 5, 5, 5]
width = 0.25
## simple way of doing this
# plt.bar(np.arange(len(drinks)), jagur,+width,align='edge',label="Jagur", color='r',edgecolor='r',yerr=jaguryerr)
# plt.bar(np.arange(len(drinks)), rangerover,-width ,align='edge',label="Range Rover", color='b',edgecolor='b',yerr=rangerovererr)
### Another way of doing same thing
plt.bar(np.arange(len(drinks)), jagur,width,align='edge',label="Jagur", color='r',edgecolor='r',yerr=jaguryerr)
plt.bar(np.arange(len(drinks))+width, rangerover,width ,align='edge',label="Range Rover", color='b',edgecolor='b',yerr=rangerovererr)
plt.bar(np.arange(len(drinks))+width+width, rangerover,width ,align='edge',label="Land Rover", color='c',edgecolor='c',yerr=rangerovererr)
print(type(np.arange(len(drinks))))
print(type(range(len(drinks))))
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('Monthly sales of cars')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-06.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
barplot-06.py
hosted with ❤ by GitHub

Output

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.