Bar Plot in Matplotlib
A bar plot uses rectangular bars to represent data categories, with bar length or height proportional to their values. It compares discrete categories, with one axis for categories and the other for values.
Consider a simple example where we visualize the sales of different fruits:
import matplotlib.pyplot as plt
import numpy as np
fruits = ['Apples', 'Bananas', 'Cherries', 'Dates']
sales = [400, 350, 300, 450]
plt.bar(fruits, sales)
plt.title('Fruit Sales')
plt.xlabel('Fruits')
plt.ylabel('Sales')
plt.show()
Output:

Simple bar plot for fruits sales
What is a Bar Plot?
A bar plot (or bar chart) is a graphical representation that uses rectangular bars to compare different categories. The height or length of each bar corresponds to the value it represents. The x-axis typically shows the categories being compared, while the y-axis shows the values associated with those categories. This visual format makes it easy to compare quantities across different groups.
This function takes several parameters:
- x: The categories (e.g., fruits).
- height: The corresponding values (e.g., sales).
- width: The width of the bars (default is 0.8).
- bottom: The baseline for the bars (default is 0).
- align: How to align bars (‘center’ or ‘edge’)
Why Use Bar Plots?
Bar plots are significant because they provide a clear and intuitive way to visualize categorical data. They allow viewers to quickly grasp differences in size or quantity among categories, making them ideal for presenting survey results, sales data, or any discrete variable comparisons.
Syntax: plt.bar(x, height, width, bottom, align)
Customizing Bar Colors
You can customize the color of the bars by using the color
parameter in the bar()
function:
import matplotlib.pyplot as plt
import numpy as np
fruits = ['Apples', 'Bananas', 'Cherries', 'Dates']
sales = [400, 350, 300, 450]
plt.bar(fruits, sales, color='violet')
plt.title('Fruit Sales')
plt.xlabel('Fruits')
plt.ylabel('Sales')
plt.show()
Output:

Changed color to Violet
Creating Horizontal Bar Plots
For horizontal bar plots, you can use the barh()
function. This function works similarly to bar()
, but it displays bars horizontally:
import matplotlib.pyplot as plt
import numpy as np
fruits = ['Apples', 'Bananas', 'Cherries', 'Dates']
sales = [400, 350, 300, 450]
plt.barh(fruits, sales)
plt.title('Fruit Sales')
plt.xlabel('Fruits')
plt.ylabel('Sales')
plt.show()
Output:

Horizontal Plots
Adjusting Bar Width
You can control the width of the bars using the width
parameter:
import matplotlib.pyplot as plt
import numpy as np
fruits = ['Apples', 'Bananas', 'Cherries', 'Dates']
sales = [400, 350, 300, 450]
plt.bar(fruits, sales, width=0.3)
plt.title('Fruit Sales')
plt.xlabel('Fruits')
plt.ylabel('Sales')
plt.show()
Output:

bar plot with low width()
Multiple bar plots
Multiple bar plots are used when comparison among the data set is to be done when one variable is changing. We can easily convert it as a stacked area bar chart, where each subgroup is displayed by one on top of the others. It can be plotted by varying the thickness and position of the bars. Following bar plot shows the number of students passed in the engineering branch:
import numpy as np
import matplotlib.pyplot as plt
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
IT = [12, 30, 1, 8, 22]
ECE = [28, 6, 16, 5, 10]
CSE = [29, 3, 24, 25, 17]
br1 = np.arange(len(IT))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
plt.bar(br1, IT, color ='r', width = barWidth,
edgecolor ='grey', label ='IT')
plt.bar(br2, ECE, color ='g', width = barWidth,
edgecolor ='grey', label ='ECE')
plt.bar(br3, CSE, color ='b', width = barWidth,
edgecolor ='grey', label ='CSE')
plt.xlabel('Branch', fontweight ='bold', fontsize = 15)
plt.ylabel('Students passed', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(IT))],
['2015', '2016', '2017', '2018', '2019'])
plt.legend()
plt.show()
Output:

Stacked bar plot
Stacked bar plots represent different groups on top of one another. The height of the bar depends on the resulting height of the combination of the results of the groups. It goes from the bottom to the value instead of going from zero to value. The following bar plot represents the contribution of boys and girls in the team.
import numpy as np
import matplotlib.pyplot as plt
N = 5
boys = (20, 35, 30, 35, 27)
girls = (25, 32, 34, 20, 25)
boyStd = (2, 3, 4, 1, 2)
girlStd = (3, 5, 2, 3, 3)
ind = np.arange(N)
width = 0.35
fig = plt.subplots(figsize =(10, 7))
p1 = plt.bar(ind, boys, width, yerr = boyStd)
p2 = plt.bar(ind, girls, width,
bottom = boys, yerr = girlStd)
plt.ylabel('Contribution')
plt.title('Contribution by the teams')
plt.xticks(ind, ('T1', 'T2', 'T3', 'T4', 'T5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('boys', 'girls'))
plt.show()
Output:

FAQs on Bar Plot in Matplotlib
What is a bar plot in Matplotlib?
A bar plot is a chart that uses rectangular bars to represent and compare categorical data
How do I create a simple bar plot using Matplotlib?
You specify categories and values, then use a function in Matplotlib to generate the plot.
Can I customize the colors of the bars in a bar plot?
- Yes, you can change the colors of the bars to enhance visual appeal and differentiation.
What parameters can I adjust in the bar()
function?
You can adjust categories, values, bar width, color, and alignment.
How can I create a horizontal bar plot?
Use a specific function in Matplotlib designed for horizontal plots, which is useful for long category names.