Get Started

Making a bar chart

1. Making a bar chart

Previously, you learned how to make a line plot and a scatter plot using matplotlib. In this lesson, you will learn how to make a bar chart.

2. Comparing pet crimes

Officer Deshaun wants to examine the average number of pet kidnappings per month across different precincts. The best way to visualize a comparison of categorical data is by using a bar chart. Creating a bar chart in matplotlib is simple. We simply provide two arguments to the function plt-dot-bar: the labels for each bar, and the height of each bar. Because the x-axis is labeled for us, we only need to add a label for the y-axis using plt-dot-ylabel.

3. Horizontal bar charts

We can also make a horizontal bar chart. We simply use the function plt-dot-barh instead of plt-dot-bar. When we have many bars, it can be easier to fit all of the labels in with a horizontal bar plot.

4. Adding error bars

In the previous exercises, we've been plotting the average number of pets kidnapped per month. Averages don't always tell the full story. Often, we'll want to show some sort of error associate with our average, such as the standard deviation or standard error of the mean. We can add error bars to our bar chart by using the keyword argument yerr after our the first two positional arguments in plt-dot-bar. In this case, we are filling in yerr with a column of our DataFrame called "error".

5. Stacked bar charts

We know that Cityville had more pet kidnappings than either Farmburg or Suburbia. Office Deshaun wonders if this is true for both cat and dog kidnappings. The best way to compare both cat and dog kidnappings while also displaying the total number of kidnappings is to use a stacked bar chart. In a stacked bar chart, we display two different sets of bars. The height of each blue bar represents the number of dogs kidnapped. The height of each orange bar represents the number of cats kidnapped. The total height of the blue and orange bars represents the total number of pets kidnapped.

6. Stacked bar charts

To create this stacked bar chart, we start by plotting dogs column as we normally would. Here we show just one of the columns, which starts at y equals 0 and has a height of 4.

7. Stacked bar charts

Next, we stack the cat bars on top of the dog bars by using the keyword "bottom". Normally, a bar chart will start each bar at 0, but if we add the keyword "bottom" to plt-dot-bar and feed in the heights of our bottom bars (in this case df-dot-dog), matplotlib will plot the second set of bars starting where the first set of bars ends. For this particular bar, the cat bar starts at a height of y equals 4 and ends at a height of y equals 10. The height of the orange bar alone is 6.

8. Stacked bar charts

This is our complete set of code for creating a bar plot. Notice that we also added the keyword argument "label" for each bar plot to create our legend, just like we did for our line plots.

9. Let's practice!

You've just learned how to create a bar chart, add errors bars, and stack bar charts to make comparisons. Let's practice!