Creating Broken Axis Plots in Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In data visualization, there are times when we have to deal with outliers that make it difficult to see the details of most of the data. In such cases, we can use a broken axis to zoom in on the majority of the data while still showing the outliers. In this tutorial, we will learn how to create a broken axis plot using Matplotlib in Python.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import the Required Libraries

We will start by importing the necessary libraries. We need Matplotlib and NumPy to create our plot.

import matplotlib.pyplot as plt
import numpy as np

Create the Data

We will now create some random data that will contain outliers. We will use numpy.random.rand to generate 30 random numbers and then add two outliers to the data.

np.random.seed(19680801)

pts = np.random.rand(30)*.2
## Now let's make two outlier points which are far away from everything.
pts[[3, 14]] += .8

Create the Subplots

Next, we will create two subplots - one for the outliers and one for the majority of the data. We will use plt.subplots to create the subplots and set the sharex parameter to True so that they share the same x-axis.

fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)

Plot the Data

We will now plot the data on both subplots using ax1.plot and ax2.plot.

ax1.plot(pts)
ax2.plot(pts)

Set the Y-Axis Limits

We will limit the y-axis of the first subplot to show only the outliers and the second subplot to show the majority of the data. We will use ax1.set_ylim and ax2.set_ylim to set the y-axis limits.

ax1.set_ylim(.78, 1.)  ## outliers only
ax2.set_ylim(0, .22)  ## most of the data

Hide the Spines

We will now hide the spines between the two subplots using ax1.spines.bottom.set_visible and ax2.spines.top.set_visible.

ax1.spines.bottom.set_visible(False)
ax2.spines.top.set_visible(False)

Adjust the Ticks

We will now adjust the ticks on the x-axis. We will move the ticks on the first subplot to the top using ax1.xaxis.tick_top, remove the tick labels on the first subplot using ax1.tick_params(labeltop=False), and keep the tick labels on the second subplot.

ax1.xaxis.tick_top()
ax1.tick_params(labeltop=False)
ax2.xaxis.tick_bottom()

Create the Cut-Out Slanted Lines

Finally, we will create the cut-out slanted lines. We will create line objects in axes coordinates and use ax1.transAxes and ax2.transAxes to transform them to the coordinates of each subplot. We will use ax1.plot and ax2.plot to plot the lines. We will also use marker to specify the marker style, markersize to set the size of the markers, linestyle to set the style of the line, color to set the color of the line, mec to set the color of the marker edge, and mew to set the width of the marker edge.

d = .5
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
              linestyle="none", color='k', mec='k', mew=1, clip_on=False)
ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)

Show the Plot

We will now show the plot using plt.show().

plt.show()

Summary

In this tutorial, we learned how to create a broken axis plot using Matplotlib in Python. We started by importing the necessary libraries and creating some random data with outliers. We then created two subplots, plotted the data on both subplots, and set the y-axis limits. We hid the spines between the subplots and adjusted the ticks on the x-axis. Finally, we created the cut-out slanted lines and showed the plot.

Other Python Tutorials you may like