Hands-on Bar Plots with Matplotlib in Python
A practical guide on how to draw effective bar plots.
Suppose that you have average salary data for five different jobs and you want to compare these salaries. You can use bar plots to visualize this data. Bar plots are used to compare values in different categories. Bars can be plotted horizontally or vertically. In this post, I’ll cover the following topics.
- How to draw a bar plot?
- How to draw error bars?
- How to draw a bar plot with error bars?
Let’s dive in!
Bar Plots
First of all, let’s import the necessary libraries.
You can find the notebook and dataset here. Next, I’m going to use the %matplotlib inline
magic command to see the plots between the lines.
I’m going to choose the seaborn-whitegrid style as the graphic style.
Let’s first create a dataset that shows the average scores of seven classes in mathematics, statistics, and computer classes to show the bar plot.
Next, let’s create the classes
variable using the list.
Let’s draw a bar plot for these classes. Here, the legend
method shows the label names in the plot.
Oops! you encounter a line plot when you run these commands. It is better to draw a bar plots if the x-axis has a categorical variable. Let’s use the bar
method instead of the plot
method to draw a bar plot.
As you can see, the stacked bars were drawn. To see the individual bars of the data, let’s first index the data. I’m going to create an x_index
variable for this.
Next, let’s add this variable to the bar
method and use an x
variable to prevent bars from overlapping.
Notice that the bars overlapped. Let’s use the width
parameter to see the bars better.
If you pay attention to the x-axis, there are numbers and if you want to replace these numbers with the values of the classes
variable, you can use the xtricks
method.
Error Bars
In scientific studies, measurements are made by taking into account the errors. Error bars are graphical representations of the variability of data and are used on graphs to indicate the error or uncertainty in a reported measurement. So you may want to use the error bars.
Let’s create values for the x and y axes. First, I’m going to take 30 numbers between 0 and 30 for the x-axis.
Next I’m going to generate 30 integers between 0 and 5 for the y-axis.
Let’s plot the scatter plot for the x and y axes.
Let’s add error bars to these points.
You can use the fmt
parameter to adjust the color and appearance. To see horizontal error bars, you can utilize the xerr
parameter.
You can add other properties to the errorbar
method.
Bar Plot with Error Bars
Let’s draw a plot to see the bar and error bars together. To demonstrate this, I’m going to use the iris dataset. Let’s import the load_iris
method to read this dataset.
In the iris dataset, there are variables that show the sepal length and width, petal height and width, and three types of iris flower. Let’s read this dataset.
Next, let’s calculate the mean and standard deviation of the numerical variables in the dataset.
Let’s take an interval variable and assign it to this interval variable the 4 numerical variables in the iris dataset.
After that, I’m going to draw two plots, one of them is a horizontal bar and the other is a vertical bar. Let’s draw error bars and horizontal bar plots in the first graphics area. The bars
method is used to plot the bars horizontally.
Let’s add a title to the plot.
You can also use the ytricks
method to label bars on the y-axis.
Next, let’s plot the vertical bars.
Let’s give the labels for the x-axis with the xtricks
method. I’m going to use the rotation
parameter to set the rotation of the texts.
In addition, you can adjust the width of the bars with the width
parameter.
Conclusion
Bar plots are used to compare values in different categories. Bars can be plotted horizontally or vertically. In this post, I talked about the bar plots. That’s it. I hope you enjoy it. Thank you for reading. You can find this notebook here.
Don’t forget to follow us on YouTube | GitHub | Twitter | Kaggle | LinkedIn
If this post was helpful, please click the clap 👏 button below a few times to show me your support 👇