Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Bar charts are good when:

Bar charts are NOT good when:

There are a lot of different libraries and methods to create bar charts. Here is a quick summary. Details are found in the examples below.

APIWhen to use
df.plot(kind='bar')There is only one value per category
df.plot.barh()There is one value per category and horizontal bars adds meaning or there are many categories to show
plt.bar()Essentially the same as df.plot, but you may not have a DataFrame handy
plt.hist()You want to count occurrences
sns.barplot()There are many values per category to be averaged. The data can be structured differently.
sns.catplot(kind='bar')Effectively the same as sns.barplot()

Positive vs Negative Bars

In this example bar chart you’ll see that the positive values have green bars while the negative values have red bars. The example below only uses two colors to emphasize the direction of change. We use a bar chart here despite there being many different years to plot because the green/red colors emphasize the relative changes nicely.

Image
Code
Data
Comments
Bar Chart

Overlayed Bars

This is a bar chart that has the values overlayed one on top of the other. The values are NOT stacked. This means that the base of the tall bar is at the bottom: the top of the bar has a height as denoted on the y-axis. This example shows how to overlay bar charts as well as to annotate the bars with a values.

Image
Plot Code
Annotation Code
Data
Comments
Bar Chart

Sorted Bars

Bar charts usually have an x-axis that doesn’t have a natural sorting order. However, sorting by the height of the bars is a valuable way to present data and insight.

Image
Code
Data
Comments
Bar Chart

Adjusted Y-Label

Bar charts usually have the y-axis start at 0. You may want to adjust the y-label to accentuate the differences, or simply to provide different insight. There are 3 different plots here. First, we use bottom on the plt.bar API to adjust the y-axis. To get the bars to draw at the correct height, we must plot an adjusted column. In this plot we also explore how to arbitrarily label the y-axis. Second, we use the plt.ylim API and adjust the list of Rectangles used to place the labels in the center of the bars. We also choose to plot on the Series object, simply to demonstrate that it can be done. Third, we label on the 'edge' which simplifies the code a bit. We also play around with different colors. Read the comments in the code to get more insight.

Image 1
Image 2
Image 3
Code 1
Code 2
Code 3
Bar Chart

Stacked Bars

Sometimes the addition of multiple values has meaning and you’ll want to stack one bar on top of another. This allows you to see multiples values and their sum total. We still sort the bars by a specific value to provide some added insight.

Image
Code
Data
Comments
Bar Chart

Side-by-side Bars

Sometimes you want to compare two values side-by-side across a set of categories. We demonstrate TWO ways to do this:

  1. plt offers a way using stacked=False where there are many columns with values to plot.

  2. seaborn allows us to plot using hue='column' when the data is structured differently.

Be sure to look at the data structure in each of these two examples.

Using plt

Image
Code
Data
Comments

Note that this image is very, very similar to the Seaborn plot generated with the code used below. The differences are: The legend does not have a title, the color shades are slightly different, the legend is fully opaque, there are vertical grid lines. All of these are very subtle.
Bar Chart

Using seaborn

We can also use Seaborn to do this plot, but the data needs to be organized differently. We do a little extra work to get the data to look right, but sometimes our data starts off looking this way. Furthermore, it is really good to know about the method pd.melt. The resulting bar plot is virtually identical.

Image
Data
Code
Comments

Note that this image is very, very similar to the plot generated with plt above.
Bar Chart

Horizontal Bar Chart

If we want to plot our bars horizontally, we can leverage the barh method.

Image
Code
Data
Comments
Bar Chart

Histogram

Sometimes you want to know the number of times something occurs. A Histogram will allow you to do a bar plot that represents counts.

Image
Code
Data
Comments
Bar Chart

Seaborn Colorful Bar Chart

Seaborn has a wide variety of fancy charts and offers easy ways to view insightful statistics. When it comes to bar charts it doesn’t add that much. Here we show two things:

  1. Fancy colors and a different background

  2. Statistical analysis when there are many values per category on the x-axis

Image
Code
Data
Comments
Bar Chart

Seaborn Statistical Bar Chart

There can be many values that fall in a specific category. In this example, we analyze how far people can throw a ball. We have 400 samples of people’s distance and gender and we show a simple bar chart showing the difference.

Image
Code
Data
Comments
Bar Chart