Matplotlib.axes.Axes.bar() in Python5 Jan 2025 | 4 min read Bar plots are commonly used to visually represent categorical data and their corresponding values. The bar() function belongs to the axes class and creates bar graphs. It is one of the most commonly used plots to explore this article's bar() function. ![]() Syntax
ExampleHere is a simple example program to create a bar graph using bar() Program Output: ![]() Explanation We have used a bar graph to plot the count of vehicles present to make it easier to visualize. To do this, we imported matplotlib.pyplot and defined the data. Next, we created a bar graph using the bar() function and passed the data as parameters. We added labels and a title to make the graph more informative, and finally, we displayed it using the show() method. Customizing the barsYou can customize the appearance of bars in a bar graph using parameters. The "color" parameter can be used to change the color of the bars. Example 1Here is an example program to customize the bars Program Output: ![]() Explanation We added additional arguments to the bar chart: color (orange) and alpha (0.9). Alpha adjusts the transparency of the color. Example 2Let us see an example program to change the bar width and edge colour. Program Output: ![]() Explanation We created a bar plot with custom bar width and edge color. We set the width with 'custom_bar_width' and the edge with 'custom_edge_color'. The resulting graph has yellow bars with green edges. Creating horizontal barsA horizontal bar graph can be achieved using the barh() function ExampleLet us see how we can place the horizontal bars in a graph Program Output: ![]() Explanation We have created a bar graph that shows the number of matches won by each team. We plot the graph with horizontal bars using the 'barh()' function. We passed the data values 'teams' and 'matches' and set the color to violet. Then, we added labels and a title before finally displaying the graph. Plotting the bar graph from the csv fileTo plot the data from the CSV file, we need to import the file, extract the data, and use the bar() function. ExampleHere is an example program for plotting the bar graph of the CSV data Program Output: ![]() Explanation In this code, we have imported two libraries; one is matplotlib for plotting, and the other one is seaborn for loading the dataset. After importing the libraries, we loaded the dataset present and extracted the days and total_bill for the plot. We plotted using the bar() function and specified the color as light green. Then, we added labels and titles as usual. Stacked plotsIf you have multiple datasets for each category, you can create stacked bar plots to visualize the cumulative effect. ExampleHere is an example program Program Output: ![]() Explanation plt.bar() function is not designed to create stacked bar plots directly. So, we used the bottom parameter to create a stacked plot indirectly. We plotted the first FA_1 marks of each student and stacked the FA_2 marks below it by specifying the bottom as FA_1. ConclusionTo sum it up, the bar() function in Matplotlib is a potent instrument for constructing bar plots, which are a prevalent type of data visualization. Bar plots are efficacious for demonstrating and contrasting categorical data, which is why they are extensively utilized in many fields, including data analysis, statistics, and scientific research. Next TopicCloud-computing-in-python |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India






