Matplotlib Tips to Instantly Improve Your Data Visualizations - According to "Storytelling wit

Author:Murphy  |  View: 22201  |  Time: 2025-03-23 18:20:51

Being able to communicate effectively with data is a skill that is relevant for anyone working with data – and not only for data scientists and data analysts.

One of my favorite books on this topic is Cole Nussbaumer Knaflic's „Storytelling with Data". It is packed with practical examples of how to improve your data visualizations.

Storytelling with Data: A Data Visualization Guide for Business Professionals

The only unfortunate aspect of the book – in my opinion – is that its examples are created using Microsoft Excel.

Raise your hand if you know an engineer that likes to create data visualizations in Excel – Yeah, me neither.

„You might be an engineer, but it shouldn't take someone with an engineering degree to understand your graph." – Cole Nussbaumer Knaflic in "Storytelling with Data"

That's why this article will cover the Matplotlib code snippets I have used most since reading Nussbaumer Knaflic's „Storytelling with Data".

import matplotlib.pyplot as plt

This article assumes you already know the basics of data visualizations with Matplotlib and Seaborn, such as creating bar plots, line plots, or scatter plots, modifying color palettes, and adding fundamental labels. This article also assumes that you are aware of when to use which type of graph.

Instead of covering the basics of Matplotlib, this article focuses on less commonly known tricks, such as:


Let's start with a simple example. The following data is fictional to allow us to focus on the data visualization techniques:

import pandas as pd  # Define fictional example dataframe df = pd.DataFrame(           {'feature 1' : ['cat 1', 'cat 2', 'cat 3', 'cat 4'],            'feature 2' : [400, 300, 200, 100]           })

Let's create a simple monochrome barplot using Seaborn with a title as a starting point:

import seaborn as sns  # Create a basic bar chart from the example dataframe fig, ax = plt.subplots(1,1, figsize = (6, 4)) sns.barplot(data =  df,              x = 'feature 1',              y = 'feature 2',              color = 'tan')  # Add title ax.set_title('Meaningful Title')  plt.show()

Remove Clutter

In the chapter "Clutter is your enemy!" Nussbaumer Knaflic talks about how to identify and eliminate visual clutter from your data visualization – this section will show you how to remove visual clutter in Matplotlib plots.

"[…E]very single element adds cognitive load on the part of your audience." – Cole Nussbaumer Knaflic in "Storytelling with Data"

How to remove the top and right border of a Matplotlib plot

By default, Matplotlib plots have a box of so-called spines around the edges of the figure. Especially the top and right spines can clutter the data visualization and thus should be removed.

You can simply remove the irrelevant spines with the following code snippet:

# Remove top and right spines ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False)

Use 'bottom' and 'left' if you want to remove the other spines as well. And if you want to remove the border, including the full x- and y-axis as well, you can use ax.axis('off').

How to remove ticks from a Matplotlib plot

Ticks are usually not considered clutter. But in some cases, as in this example, the ticks of the x-axis of a bar chart are redundant.

# Remove ticks on x-axis ax.tick_params(bottom = False)

Use left = False if you want to remove the ticks of the y-axis as well.


Now, the example with removed clutter looks like follows:

Before and after removing clutter in Matplotlib plot.

Doesn't the new plot give you a much calmer feeling due to the elimination of the visual clutter?

De-Emphasize

In the chapter "Think like a designer", Nussbaumer Knaflic shows us how to mute necessary but relevant information. This section shows you how to change the colors of less important parts of a Matplotlib plot.

"Push necessary, but non-message-impacting items to the background. […] Light grey works well for this."— Cole Nussbaumer Knaflic in "Storytelling with Data"

How to customize the individual bar colors in a Matplotlib plot

Replace the color parameter of the sns.barplot method with the palette parameter to control each bar's color. By doing this, you can use light grey to de-emphasize less important bars and only highlight the relevant bar with the main color.

# Define colors of individual bars custom_colors = ['lightgrey', 'tan', 'lightgrey', 'lightgrey']  # De-emphasize less important bars sns.barplot(data =  df,              x = 'feature 1',              y = 'feature 2',              palette = custom_colors) 

How to change the color of the x- and y-axis in a Matplotlib plot

Next, we also want to mute the colors of the x- and y-axis. To do so, we need to mute the color of the axis' spine, ticks, and labels:

# Mute colors of spines ax.spines['left'].set_color('grey')    ax.spines['bottom'].set_color('grey')  # Mute colors of ticks ax.tick_params(colors = 'grey')  # Mute colors of labels ax.set_xlabel('feature 1', color = 'grey') ax.set_ylabel('feature 2', color = 'grey')

Now, the example with de-emphasized less important information looks like follows:

Before and after de-emphasizing less important information in the Matplotlib plot.

Text Is Your Friend: Adding Annotations

Nussbaumer Knaflic highlights that you should add text to your data visualizations to highlight the key takeaways. In this section, we will look at the ax.annotate() method to add text in Matplotlib plots.

"If there is a conclusion you want your audience to reach, state it in words." – Cole Nussbaumer Knaflic in "Storytelling with Data"

How to add text annotations to a Matplotlib plot

To add text to a Matplotlib figure, you can use the ax.annotate() method, which takes the text and its location in the plot as arguments. Additionally, you can specify aspects like the horizontal (ha) or vertical alignment (va) or font size.

# Add text annotations ax.annotate('Look at "cat 2". nThis is important!',              xy = (1.5, 360),                ha = 'center',               fontsize = 11,            )

If you want to have an additional arrow to point toward something, you need to use the following parameters:

  • xy: the point to annotate – a.k.a. where your arrow will point towards
  • xytext: where to place the text (and where the arrow ends)
  • arrowprops = {'arrowstyle' : '->'}: what the arrow should look like

How to add values to a bar chart in a Matplotlib plot

To add the value to every individual bar, we need to iterate over the ax.patches. For every bar, you can use the get_height(), get_width(), and get_x() methods to place the value above the bar.

# Annotate bar chart with values for bar in ax.patches:     ax.annotate(int(bar.get_height()),                 xy = (bar.get_x() + bar.get_width() / 2, bar.get_height()),                  ha = 'center',                  va = 'center',                 xytext = (0, 8),                 textcoords = 'offset points'                 )

Now, the example with added text annotations looks like follows:

Before and after adding text annotations in the Matplotlib plot.

Emphasize

In the chapter "Focus your audience's attention", Nussbaumer Knaflic talks about how to leverage preattentive attributes to guide your audience's attention to what you want them to see. In this section, we will discuss a few simple tweaks you can apply to text annotations in Matplotlib plots to leverage preattentive attributes in text.

"[…I]f we use preattentive attributes strategically, they can help us enable our audience to see what we want them to see before they even know they're seeing it." – Cole Nussbaumer Knaflic in "Storytelling with Data"

How to make whole or parts of text bold in Matplotlib annotation

Using bold text can help highlight important parts of your data visualization. If you want to only highlight part of your annotation, you can use $bf{}$ in your string and place the text to be emphasized in the curly brackets. If you want to highlight the whole annotation, just add the parameter fontweight = 'bold'.

# Make only part of text bold ax.annotate('Look at "cat 2". nThis is $bf{important}$!',              #...            )  # Make all of the text bold ax.annotate('Look at "cat 2". nThis is important!',              #...             fontweight='bold',            )

How to color text in Matplotlib annotation

To associate specific texts with specific elements in the data visualization, you can leverage the association by the same color. To give the text annotation color, just add the parameter color to the ax.annotate() method.

# Remove ticks on x-axis ax.tick_params(bottom = False) # Add important take away to plot  ax.annotate('Look at "cat 2". nThis is $bf{important}$!', # Emphasize important terms             xy = (1.5, 360),              ha = 'center',             color = 'tan',              fontsize = 11,            )

Now, the example with emphasized important information looks like follows:

Before and after emphasizing important information in the Matplotlib plot.

Summary

Below you can see the difference these small Matplotlib tricks make to the data visualization. By simply removing clutter, adding text annotations, and de-[emphasizing](#4c12) less important information and instead emphasizing important information, you can instantly see a strong difference in the readability of the example data visualization.

Before and after applying important information in the Matplotlib plot.

Below you can see the code that creates the final data visualization.

import matplotlib.pyplot as plt import seaborn as sns  # Define color palette highlight_color = 'tan' muted_color = 'dimgrey' muted_color2 = 'lightgrey' custom_colors = [muted_color2, 'tan', muted_color2, muted_color2]  # Create a basic bar chart from the example dataframe fig, ax = plt.subplots(1,1, figsize = (6, 4))  sns.barplot(data =  df,              x = 'feature 1',              y = 'feature 2',              palette = custom_colors) # De-emphasize less important bars  # Add title ax.set_title('Meaningful Title')  # Mute colors of labels ax.set_xlabel('feature 1', color = muted_color) ax.set_ylabel('feature 2', color = muted_color)  # Remove unimportant spines and mute color of remaining spines ax.spines['right'].set_visible(False)      # Remove top and right spines ax.spines['top'].set_visible(False)        # Remove top and right spines  ax.spines['left'].set_color(muted_color)   # Mute colors of spines ax.spines['bottom'].set_color(muted_color) # Mute colors of spines  # Remove ticks on x-axis and mute colors of ticks ax.tick_params(bottom = False,        # Remove ticks on x-axis     colors = muted_color,             # Mute colors of ticks )  # Annotate bar chart with values for i, bar in enumerate(ax.patches):     ax.annotate(int(bar.get_height()),     xy = (bar.get_x() + bar.get_width() / 2, bar.get_height()),      ha = 'center',      va = 'center',     xytext = (0, 8),     textcoords = 'offset points',     color = custom_colors[i])  # Add important take away to plot  ax.annotate('Look at "cat 2". nThis is $bf{important}$!', # Emphasize important terms             xy = (1.5, 360),              ha = 'center',             color = highlight_color,              fontsize = 11,            )  plt.show()

Cole Nussbaumer Knaflic's "Storytelling with Data" is one of my favorite books on data visualizations. If you are interested in how to take your data visualizations to the next level, I definitely recommend this book.

If you are interested in more Matplotlib tricks, in this repository, Andre Gaskov has recreated many visualizations from the book in Python using Matplotlib:

GitHub – empathy87/storytelling-with-data: Plots from the book "Storytelling with data"…


Enjoyed This Story?

Subscribe for free to get notified when I publish a new story.

Get an email whenever Leonie Monigatti publishes.

Find me on LinkedIn, Twitter, and Kaggle!

References

Image References

If not otherwise stated, all images are created by the author.

Web & Literature

Knaflic, Cole. Storytelling With Data: A Data Visualization Guide for Business Professionals, Wiley, © 2015.

Tags: Data Science Data Visualization Programming

Comment