• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Sparse Matrix in Python / Spy to visualize sparse matrix / How To Visualize Sparse Matrix in Python?

How To Visualize Sparse Matrix in Python?

February 28, 2019 by cmdlinetips

When you work with sparse matrix data structure with SciPy in Python, sometimes you might want to visualize the sparse matrix. A quick visualization can reveal the pattern in the sparse matrix and can tell how “sparse” the matrix is. And it is a great sanity check.

One way to visualize sparse matrix is to use 2d plot. Python’s matplotlib has a special function called Spy for visualizing sparse matrix. Spy is very similar to matplotlib’s imshow, which is great for plotting a matrix or an array as an image. imshow works with dense matrix, while Spy works with sparse matrix.

Let us first load the modules needed to make sparse matrix and visualize it. We will be using sparse module in SciPy to create sparse matrix and matplotlib’s pyplot to visualize

import matplotlib.pylab as plt
import scipy.sparse as sparse

Let us create simple sparse matrix, here a diagonal sparse matrix with ones along the diagonal with sparse.eye function. We can use the spy function with the sparse matrix as an argument.

# create a sparse diagonal matrix with ones on the diagonal
A = sparse.eye(100)
# visualize the sparse matrix with Spy
plt.spy(A)

It will create 2-D image with blue color squares representing non-zero elements and white color for elements zeros. Since our matrix is diagonal matrix, we see a blue line along the diagonal.

Visualizing Sparse Diagonal Matrix
Visualizing Sparse Diagonal Matrix

Let us create sparse matrix with a specific density

# create a sparse matrix with specific density
A = sparse.random(100,100, density=0.01)
# visualize the sparse matrix with Spy
plt.spy(A)

And visualize this 100×100 sparse matrix with the density 1%.

Sparse Matrix with 1% Density
Sparse Matrix with 1% Density

You can see the blue square is kind of big. We can control the size of the blue squares with the argument “markersize” as shown below. This will help us get a real sense of the actual sparsity of sparse matrix.

A = sparse.random(100,100, density=0.01)
plt.spy(A, markersize=4)

Here is image of same sparse matrix, but with smaller markersize. Now we get a smaller blue square representing the non-zero items and a better sense of the sparsity.

Visualizing sparse matrix  with varying markersize
Visualizing sparse matrix: Example 3

Let us create a bigger sparse matrix of dimension 10k x 10k with density of 0.00001.

A = sparse.random(10000,10000, density=0.00001)
plt.spy(A, markersize=1)

Now the visualization of the sparse matrix using Spy is much better with a smaller markersize=1.

Visualizing Sparse matrix with Spy: Example 4
Visualizing Sparse matrix with Spy: Example 4

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Create Random Sparse Matrix of Specific Density? Default ThumbnailHow To Slice Rows and Columns of Sparse Matrix in Python? Default ThumbnailHow To Save Sparse Matrix in Python to Mtx and Npz file Default Thumbnail3 Ways To Create Sparse Matrix in COO Format with SciPy

Filed Under: Spy to visualize sparse matrix, Visualizing Sparse Matrix Tagged With: Sparse Matrix in SciPy, Spy to Visualize Sparse Matrix, Visualizing Sparse Matrix

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version