Matplotlib – Bar Plot

To draw a Bar Plot using Matplotlib, call matplotlib.pyplot.bar() function, and pass required values (X-axis, and bar heights) for the bar plot.

The definition of matplotlib.pyplot.bar() function is

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

Example

In the following program, we will take some sample data for X-axis and height of the bars to draw a bar plot.

The data for X-axis of bar plot is [1, 2, 3, 4, 5].

The data for height of the bars (Y-axis) in the plot is [10, 8, 12, 4, 7].

example.py

import matplotlib.pyplot as plt

#data
x = [1, 2, 3, 4, 5]
h = [10, 8, 12, 4, 7]

#bar plot
plt.bar(x, height = h)

plt.show()

Output

Matplotlib - Bar Plot

Conclusion

In this Matplotlib Tutorial, we learned how to draw a bar plot using Matplotlib PyPlot API.