matplotlib
  1. matplotlib-bar-plots

Bar Plots - ( Basic Matplotlib )

Heading h2

Syntax

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

Example

import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D", "E"])
y = np.array([25, 45, 60, 15, 10])

plt.bar(x, y)
plt.show()

Output

A bar plot is displayed with the bars representing the values of the respective categories.

Explanation

A bar plot is a chart that displays categories as rectangular bars with the height proportional to the value they represent. Matplotlib provides bar() function in order to create bar plots. It accepts an array of values and x-axis labels as arguments to create a bar plot.

In the example given above, we create two numpy arrays, one containing the labels "A", "B", "C", "D" and "E", and the other containing the values 25, 45, 60, 15 and 10 respectively. We pass these two arrays as arguments to bar() function, which creates a bar plot with bars representing the respective values of the categories.

Use

Bar plots are used to organize and display categorical data. They are particularly useful in comparing different categories with each other. Bar plots are used in order to easily identify trends and patterns in categorical data.

Important Points

  • A bar plot is a chart that displays categories as rectangular bars with height proportional to the value they represent.
  • The bar() function of the Matplotlib library is used to create bar plots.
  • The bar() function accepts the array of values and x-axis labels as arguments.
  • Bar plots can be used to easily compare different categories with each other.
  • They are particularly useful for identifying patterns and trends in categorical data.

Summary

Bar plots are a useful visualization tool for displaying and comparing categorical data. They are easy to interpret and provide a clear visual representation of patterns and trends in the data. Matplotlib provides the bar() function to create bar plots. By using various options, we can customize bar plots according to our requirements.

Published on: