Matplotlib Visualization with Dropped Spines

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create a visualization using Matplotlib with "dropped spines". Dropped spines refers to the visualization technique where the spines of the axes (the lines around the plot) are moved to the outer edges of the plot area.

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} matplotlib/figures_axes -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} matplotlib/heatmaps -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/booleans -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/lists -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/tuples -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/importing_modules -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/standard_libraries -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/math_random -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/numerical_computing -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} python/data_visualization -.-> lab-48953{{"`Matplotlib Visualization with Dropped Spines`"}} end

Import Libraries

First, we need to import the necessary libraries. We will be using the Matplotlib library and NumPy for generating random data.

import matplotlib.pyplot as plt
import numpy as np

Set the Random Seed

For reproducibility, we will set the random seed using NumPy.

np.random.seed(19680801)

Create a Figure and Axes

We will create a figure and an axes object using plt.subplots(). The imshow() function is used to display the random data as an image.

fig, ax = plt.subplots()

image = np.random.uniform(size=(10, 10))
ax.imshow(image, cmap=plt.cm.gray)
ax.set_title('dropped spines')

Offset the Spines

We will move the left and bottom spines outward by 10 points using the set_position() function. The argument for set_position() is a tuple with two elements. The first element represents the position of the spine, and the second element represents the distance from the spine to the plot area.

ax.spines[['left', 'bottom']].set_position(('outward', 10))

Hide the Top and Right Spines

We will hide the top and right spines using the set_visible() function.

ax.spines[['top', 'right']].set_visible(False)

Display the Plot

Finally, we will display the plot using plt.show().

plt.show()

Summary

In this lab, we learned how to create a visualization using Matplotlib with "dropped spines". We used the set_position() function to move the left and bottom spines outward and the set_visible() function to hide the top and right spines. This technique is useful for improving the clarity and aesthetic of plots.

Other Python Tutorials you may like