VOOZH about

URL: https://www.geeksforgeeks.org/pandas/bar-plot-in-matplotlib/

⇱ Bar Plot in Matplotlib - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Bar Plot in Matplotlib

Last Updated : 12 Jul, 2025

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:

Output:

👁 Screenshot-2024-12-06-161448
Simple bar plot for fruits sales

What is a Bar Plot?

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:

Output:

👁 Screenshot-2024-12-06-165812
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:

Output:

👁 Screenshot-2024-12-06-170139
Horizontal Plots

Adjusting Bar Width

You can control the width of the bars using the width parameter:

Output:

👁 Screenshot-2024-12-06-170416
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:

Output:

👁 Image

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.

Output:

👁 Image
Comment
Article Tags:

Explore