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.
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 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")