I have a sparse matrix (140 x 363) with many zeroes. This matrix only has positive float values.
I would like to generate a scatter pairs matrix plot discarding all zero values (0.0, 0.0) without affecting the display of the example values (0.0, 7.2), (7.2, 0.0), etc.
I have investigated sns.pairplot
, sns.pairgrid
, pd.scatter_matrix
, Altair, and Plotty without getting a solution.
Unfortunately, none of these tools accept crs, coo, etc.
I have also tried plotting with the full matrix by passing nonzero instructions to the dataframe but it doesn't work either.
Is there a solution?
Can't you convert your sparse matrix to a coo format and then take the x and y coordinates?
import numpy as np
import pandas as pd
import seaborn as sns
from scipy.sparse import csr_matrix, coo_matrix
# demo data
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1.1, 2.2, 0, 4.4, 5.5, 6.6])
# sparse matrix
sparse_matrix = csr_matrix((data, (row, col)), shape=(3, 3))
# to coo
sparse_matrix = sparse_matrix.tocoo()
# remove point which value are zero
sparse_matrix.eliminate_zeros()
# convert to pandas
df = pd.DataFrame({'x': sparse_matrix.row,
'y': sparse_matrix.col,
'value': sparse_matrix.data})
# plot
sns.scatterplot(data=df, x="x", y="y")