How to adjust the bar width of bar plots in Matplotlib
Overview
A bar chart is a diagram in which the numerical values of variables are represented by the height or length of lines or rectangles of equal width. Simply put, they present categorical data using rectangular bars.
Bars could be horizontal or vertical. We can plot or draw the bar charts with the pyplot module in the matplotlib library.
How to create a bar chart
To plot a vertical bar chart, we use the following command:
pyplot.bar(x, y)
To plot a horizontal bar chart, we use the following command:
pyplot.barh(x, y)
In both cases, the x and y parameter values represent the x (horizontal) and y (vertical) axis of the respective plot. The only difference in both syntaxes is the barh rather than the bar only.
Code
Let’s create a vertical bar chart:
# importing the necessary libraries and modulesimport matplotlib.pyplot as pltimport numpy as np# creating the data values for the vertical y and horisontal x axisx = np.array(["Oranges", "Apples", "Mangoes", "Berries"])y = np.array([3, 8, 1, 10])# using the pyplot.bar funtionplt.bar(x,y)# to show our graphplt.show()
Explanation
In the code above:
-
Lines 2 and 3: We import the libraries (
matplotlib), and modules (pyplotandnumpy) needed to plot a bar chart. -
Lines 6 and 7: We create an array of values for the
xandyaxes withnumpy.array(). -
Line 11: We use the
pyplot.bar(x, y)function to create a vertical bar chart with horizontal and vertical values. -
Line 14: We use the
pyplot.show()function to tell Python to show us our graph.
How to adjust the bar width
The bar width helps to give a unique description or visual to a plot.
The default value for the bar width of a plot in matplotlib is 0.8. However, we can change this to any suitable size of our choice.
How to change the bar width of a plot
The steps to take when changing the bar width of a plot are listed below:
- We import the modules and libraries we need for the plot, i.e.,
matplotlib.pyplotandnumpy. - We create the data sets to be plotted.
- In the
plot()method, after declaring thewidthparameter, we assign it to any number value we want to represent the desired width of the plot. - We use the
show()method to show our plot.
The code below uses a thin width size of 0.1:
pyplot.plot(x, y, width='0.1')
Code
Let’s illustrate how to change the bar width:
# Importing the necessary libraries and modulesimport matplotlib.pyplot as pltimport numpy as np# creating our data valuesx = np.array(["A", "B", "C", "D"])y = np.array([3, 8, 1, 10])# creating our Bar chart with a width size of 0.1plt.bar(x, y, width = 0.1)# asking python to show us our plotplt.show()
Explanation
In the code above:
-
Lines 2 and 3: We import the required libraries and modules (
matplotlib.pyplotandnumpy) to plot a bar chart. -
Lines 6 and 7: We create an array of values for the
xandyaxes usingnumpy.array(). -
Line 11: We use the
pyplot.bar(x, y)function to create a vertical bar chart with horizontal and vertical values. We also use thewidthparameter value as0.1. -
Line 14: We use the
pyplot.show()function to tell Python to show us our graph.