Matplotlib vs. Bokeh - 7 Charts You Must Know How to Plot
By: Saul Dobilas
Tags: Bokeh, Python, Programming, Matplotlib, Data Visualization

Photo by fauxels from Pexels

Data visualization can be a pain but it is a necessary skill to master for people in the data science and analytics community. Luckily there are plenty of tools and libraries at our disposal to help us with this task.

In this article, I will take you through examples of how to draw charts using two of the most popular Python visualization libraries: Matplotlib and Bokeh. In addition to learning how to use these libraries, a comparison of the two will help us see which one is better suited for different types of tasks.

Charts covered in this article:

Histogram Vertical Bar & Horizontal Bar Vertical Stacked Bar & Horizontal Stacked Bar Line Area & Area Stacked Pie & Donut Scatter & Scatter Bubble

Setup

To make it a bit more fun, I will use a dataset that contains Formula 1 race winners between 1990 and 2019. Hence, all the examples below will take data from a Pandas DataFrame with some data manipulations where necessary. Note, the entire notebook together with the data can be found on my Github repository. However, you don't need to download it as I have included all the code snippets and relevant output in this article.

To start with, we will import Python libraries that we will need to use.

Note, for Bokeh library we can choose in advance whether we want our charts to be displayed inside the notebook or to be saved in an HTML file. In this case, we want charts to be displayed in our Jupyter notebook, so we will call the output_notebook(). We only need to call this once, and all subsequent calls to show() will display charts inline in the notebook.

Next, we need to import the data before we can start creating those wonderful charts.

This is what the data looks like. Each row represents one race and shows who the race winner was, how many laps the race had, and how long it took to complete it.

1. Histogram

To draw histograms we need a continuous variable. Let us use “Laps” column which will give us a distribution of laps between different races. Note, a typical F1 race distance is ~300km. Hence, shorter tracks would have more laps and longer tracks would have fewer laps required to make up that same race distance.

Matplotlib

Bokeh

Note, there is no existing histogram method in Bokeh, hence we will first need to use Numpy to create bins, which is then used to draw a histogram.

2. Vertical Bar & Horizontal Bar

Bar charts are similar to histograms but they can also take categorical variables. For our bar charts, we will use “Winner” column which will show us the number of races won by each driver between 1990 and 2019.

Before we can plot it, we need to do some aggregation of the data. It is simple data manipulation in Pandas with the result being two new DataFrames: one with race winners ordered from lowest to highest win count and the other one being the same but ordered from highest to lowest win count.

Below is a snippet of the resulting data:

Matplotlib

First, we plot a vertical bar chart:

Followed by the horizontal bar chart:

Bokeh

Vertical:

And then horizontal:

As you can see, the results are pretty similar and it is pretty easy to plot bar charts with both libraries.

3. Vertical Stacked Bar & Horizontal Stacked Bar

To draw stacked bar charts, we will need to use an additional dimension from our data. We will keep race winners and also add the team they drove for at the time they won those GPs.

The easiest way to get the data in the required format is by using Pandas crosstab function. This will create a matrix that will enable us to plot stacked charts with minimal coding.

Here is the snippet of the resulting data:

We can now plot our stacked bar charts.

Matplotlib

Vertical:

Horizontal:

Bokeh

Vertical:

Horizontal:

4. Line Charts

Line charts are being used everywhere and they are usually most suitable for displaying trends.

Again, we will need to do a little bit of data prep before we can plot it. For our case, we will select the Monaco Grand Prix and plot the amount of time it took to complete this race in each of the last 30 years. Note, race times can vary significantly, especially when they are affected by bad weather conditions or major incidents.

The resulting DataFrame looks like this:

As before let us plot it with Matplotlib first, followed by Bokeh.

Matplotlib

Bokeh

5. Area & Area Stacked

For the area chart with one variable, we will use the same data that we used to plot line charts. Meanwhile, for stacked area charts we will add an additional race track (Great Britain).

Below is the preparation of the data for stacked area charts:

Note, the resulting DataFrame is largely identical to the one in the line chart data preparation but simply contains extra rows for Great Britain. Hence, you can refer to the picture in the previous section if you would like to see what the DataFrame looks like.

As before, let us plot a single area chart first, followed by a stacked area chart:

Matplotlib

Area:

Stacked area:

Bokeh

Area:

Stacked Area:

If you looked at the code carefully, you must have noticed that there was a little bit of extra work required to get the data in the right format for Bokeh stacked area charts. However, after that, it was just as easy to plot it with Bokeh as it was with Matplotlib.

6. Pie & Donut Charts

There is an old joke which says that every PowerPoint presentation should contain at least one pie chart. Whether you agree with that or not, pie charts are extremely common, hence it is very important to know how to plot them.

For this example, we will use the top 5 drivers with the most wins and we will group the rest of them into ‘Other Drivers’ category.

Our resulting DataFrame is pretty simple and looks like this:

Let us now plot some pies and donuts.

Matplotlib

Pie chart:

Donut:

Bokeh

Unfortunately, Bokeh does not have a high-level method to plot pie and donut charts. Hence, we will have to use a “wedge” glyph, which means a bit of extra work will be necessary. This includes setting the angles for each wedge and adding color values into a DataFrame.

This is what our updated DataFrame looks like:

Pie chart:

Donut:

Note, I left the grid visible so it is easy for you to see how we are drawing these charts. Also, there is no easy way in Bokeh to add labels to pie and donut charts. One way would be to manually specify chart coordinates for each one of the labels. If you wanted something a bit more automated then you would have to use some trigonometry to calculate the coordinates based on the wedge angles.

What is much simpler though, is enabling labels to show when you hover with your mouse. This is what the “tooltips=” in the code is for. However, this only works when your chart is being rendered in HTML and not when it is exported as png (as the case for a picture used in this article).

7. Scatter & Scatter Bubble

Finally, let us draw some scatter plots. For this, we will take the 2019 season from our DataFrame and use “Laps” together with a slightly modified “Time” column.

Due to the difference in track lengths, there will not be much correlation between the two metrics but this is for illustration purposes only. Note, we will also need to convert “Time” to float as we did for line charts.

The resulting DataFrame is not much different from the original one. It simply contains a few additional columns that we will need to use for scatter plots.

Matplotlib

Scatter:

Scatter Bubble:

Bokeh

Scatter:

Scatter Bubble:

Verdict

After plotting all of these charts we can evaluate both libraries based on how the charts look and also the ease of use.

Personally, I prefer the way Bokeh charts look. Out of the box, they simply look nicer and have higher resolution. It is, of course, possible to customize Matplotlib charts to make them look better but that carries the price of having to do some extra coding.

As for the ease of use, the point goes to Matplotlib. It is primarily due to the much easier way to plot pie and donut charts as well as the ability to plot histograms without requiring to use NumPy.

To summarize, it often comes to preference and familiarity. However, we have uncovered that each of the libraries has its own pros and cons.

I hope that this comparison of the 7 most common charts in Matplolib and Bokeh will help you choose the right library for you.

Cheers!

SolClover