Matplotlib Tips to Instantly Improve Your Data Visualizations — According to “Storytelling with Data” | by Leonie Monigatti | Jun, 2023


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()

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.



Source link

Leave a Comment