Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

How to adjust the bar width of bar plots in Matplotlib

Onyejiaku Theophilus Chidalu

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 modules
import matplotlib.pyplot as plt
import numpy as np

# creating the data values for the vertical y and horisontal x axis
x = np.array(["Oranges", "Apples", "Mangoes", "Berries"])
y = np.array([3, 8, 1, 10])


# using the pyplot.bar funtion
plt.bar(x,y)

# to show our graph
plt.show()
Creating a bar chart

Explanation

In the code above:

  • Lines 2 and 3: We import the libraries (matplotlib), and modules (pyplot and numpy) needed to plot a bar chart.

  • Lines 6 and 7: We create an array of values for the x and y axes with numpy.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:

  1. We import the modules and libraries we need for the plot, i.e., matplotlib.pyplot and numpy.
  2. We create the data sets to be plotted.
  3. In the plot() method, after declaring the width parameter, we assign it to any number value we want to represent the desired width of the plot.
  4. 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 modules
import matplotlib.pyplot as plt
import numpy as np

# creating our data values
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])


# creating our Bar chart with a width size of 0.1
plt.bar(x, y, width = 0.1)

# asking python to show us our plot
plt.show()
Creating a bar chart using the width parameter

Explanation

In the code above:

  • Lines 2 and 3: We import the required libraries and modules (matplotlib.pyplot and numpy) to plot a bar chart.

  • Lines 6 and 7: We create an array of values for the x and y axes using numpy.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 the width parameter value as 0.1.

  • Line 14: We use the pyplot.show() function to tell Python to show us our graph.

RELATED TAGS

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring