• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

Bar Plots with Matplotlib in Python

datavizpyr · February 10, 2020 ·

In this post, we will see how to make bar plots with Matplotlib in Python. We will first start with making simple bar plot in matplotlib and then see how to make bar plots ordered in ascending and descending order.

Let us load Pandas and matplotlib to make bar charts in Python.

import matplotlib.pyplot as plt
import pandas as pd

Let us create some data for making bar plots. We will use the Stack Overflow Survey data to get approximate average salary and education information. We have the salary and educational qualification as two lists.

education = ["Bachelor's", "Less than Bachelor's","Master's","PhD","Professional"]
# mean annual salary in US dollars
salary = [110000,105000,126000,144200,95967]

Let us store the data as Pandas dataframe using pd.DataFrame function by providing the lists as dictionary.

# create Pandas dataframe from two lists
df = pd.DataFrame({"Education":education,
                  "Salary":salary})

It is a small dataframe with 5 rows and 2 columns.

df

       Education	Salary
0	Bachelor's	110000
1	Less than Bachelor's	105000
2	Master's	126000
3	PhD	144200
4	Professional	95967

Simple Bar Plot with Matplotlib

Let us make a simple bar plot using matplotlib.pyplot in Python. In matplotlib, we can make barplot with bar() function. In this example, we specify Education on x-axis and salary on y-axis from our dataframe to bar() function.

plt.figure(figsize=(10,6))
# make bar plot with matplotlib
plt.bar('Education', 'Salary',data=df)
plt.xlabel("Education", size=15)
plt.ylabel("Salary in US Dollars", size=15)
plt.title("Barplot with Matplotlib", size=18)
plt.savefig("bar_plot_matplotlib_Python.png")

We have customized the barplot with x and y-axis labels and title for the bar plot. We get a simple barplot made with matplotlib. By default, matplotlib.pyplot chooses blue color to fill the bars of barplot.

Simple Bar Plot with Matplotlib
How to Make Bar Plot with Matplotlib?

How To Make Bar Plot in Ascending Order with Matplotlib?

Note that in the simple bar plot we made, bars are in the same order as it was in our dataframe. Sometimes it is better to order the bars of barplot in Ascending or Descending order.

Let us first see how to make bar plots ordered in ascending order with Matplotlib. To order the bars in ascending order, we first need to sort our dataframe in ascending order. We can use Pandas’ sort_values() function to order by Salary variable.


df_sorted= df.sort_values('Salary')

Pandas sort_values() function orders the dataframe in ascending order by default.

df_sorted

       Education	Salary
4	Professional	95967
1	Less than Bachelor's	105000
0	Bachelor's	110000
2	Master's	126000
3	PhD	144200

Now we can use the sorted dataframe with our bar() function to make barplot ordered in ascending order.

plt.figure(figsize=(10,6))
# bar plot with matplotlib
plt.bar('Education', 'Salary',data=df_sorted)
plt.xlabel("Education", size=15)
plt.ylabel("Salary in US Dollars", size=15)
plt.title("Bar plot in Ascending Order with Matplotlib", size=18)
plt.savefig("bar_plot_matplotlib_ascending_order_Python.png")

And we get barplot in ascending order with Matplotlib.

How to Make Bar Plot in Ascending with Matplotlib?
Bar Plot Ascending Order with Matplotlib

How To Make Bar Plot in Descending Order with Matplotlib?

To create barplot with bars ordered in descending order, we first sort the dataframe such that it is ordered in descending order. To sort the Pandas dataframe, we need to use ascending=False argument with Pandas sort_values() function.

df_sorted_desc= df.sort_values('Salary',ascending=False)

We get dataframe ordered in descending order. And we can use the sorted dataframe to make barplot in descending order.

plt.figure(figsize=(10,6))
# bar plot with matplotlib
plt.bar('Education', 'Salary',data=df_sorted_desc)
plt.xlabel("Education", size=15)
plt.ylabel("Salary in US Dollars", size=15)
plt.title("Bar plot in Descending Order with Matplotlib", size=18)
plt.savefig("bar_plot_matplotlib_descending_order_Python.png")
How to Make Bar Plot in Descending Order with Matplotlib?
Bar Plot in Descending Order with Matplotlib

Related posts:

How To Adjust Axes Label Position in Matplotlib?How To Adjust Positions of Axis Labels in Matplotlib? Add Rectangle with text annotation MatplotlibHow To Draw a Rectangle on a Plot in Matplotlib? Connect Paired Points with Lines in MatplotlibHow To Connect Paired Data Points with Lines in Scatter Plot with Matplotlib Matplotlib Scatter plot with legendHow to Add Legend to Scatterplot Colored by a Variable with Matplotlib in Python

Filed Under: bar plot Matplotlib, Matplotlib, Python Tagged With: barplot, Matplotlib, Python

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version