Plotting Sparsity Patterns

PythonPythonBeginner
Practice Now

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

Introduction

In this tutorial, we will learn how to plot sparsity patterns of arrays using Matplotlib. The sparsity pattern refers to the distribution of non-zero elements in an array. We will use the spy function in Matplotlib to plot the sparsity patterns.

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`"]) 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`") 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-48955{{"`Plotting Sparsity Patterns`"}} matplotlib/figures_axes -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} python/lists -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} python/tuples -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} python/importing_modules -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} python/standard_libraries -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} python/math_random -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} python/numerical_computing -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} python/data_visualization -.-> lab-48955{{"`Plotting Sparsity Patterns`"}} end

Importing Required Libraries

We will start by importing the required libraries which are NumPy and Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Creating Random Array

Next, we will create a random array with dimensions (20, 20) using the numpy.random.randn function. We will also set a few elements to zero to create a sparse matrix.

np.random.seed(19680801)
x = np.random.randn(20, 20)
x[5, :] = 0.
x[:, 12] = 0.

Creating Subplots

We will now create a 2x2 grid of subplots using the subplots function. This will give us four plots to visualize the sparsity pattern of the array.

fig, axs = plt.subplots(2, 2)
ax1 = axs[0, 0]
ax2 = axs[0, 1]
ax3 = axs[1, 0]
ax4 = axs[1, 1]

Plotting Sparsity Pattern

We will use the spy function to plot the sparsity pattern of the array. We will use different parameters such as markersize and precision to customize the plot.

ax1.spy(x, markersize=5)
ax2.spy(x, precision=0.1, markersize=5)
ax3.spy(x)
ax4.spy(x, precision=0.1)

Displaying the Plots

Finally, we will display the plots using the show function.

plt.show()

Summary

In this tutorial, we learned how to plot sparsity patterns of arrays using Matplotlib. We used the spy function to visualize the sparsity pattern and customized the plot using different parameters. We also learned how to create subplots and display the plots using the show function.

Other Python Tutorials you may like