1

I have a sparse matrix X, shape (6000, 300). I'd like something like a scatterplot which has a dot where the X(i, j) != 0, and blank space otherwise. I don't know how many nonzero entries there are in each row of X. X[0] has 15 nonzero entries, X[1] has 3, etc. The maximum number of nonzero entries in a row is 16.

Attempts:

  • plt.imshow(X) results in a tall, skinny graph because of the shape of X. Using plt.imshow(X, aspect='auto) will stretch out the graph horizontally, but the dots get stretched out to become ellipses, and the plot becomes hard to read.
  • ax.spy suffers from the same problem.
  • bokeh seems promising, but really taxes my jupyter kernel.

Bonus:

  • The nonzero entries of X are positive real numbers. If there was some way to reflect their magnitude, that would be great as well (e.g. colour intensity, transparency, or across a colour bar).
  • Every 500 rows of X belong to the same class. That's 12 classes * 500 observations (rows) per class = 6000 rows. E.g. X[:500] are from class A, X[500:1000] are from class B, etc. Would be nice to colour-code the dots by class. For the moment I'll settle for manually including horizontal lines every 500 rows to delineate between classes.

3 Answers 3

1

You can use nonzero() to find the non zero elements and use scatter() plot the points:

import pylab as pl
import numpy as np

a = np.random.rand(6000, 300)
a[a < 0.9999] = 0
r, c = np.nonzero(a)
pl.scatter(r, c, c=a[r, c])
Sign up to request clarification or add additional context in comments.

Comments

0

It seems to me heatmap is the best candidate for this type of plot. imshow() will return u a colored matrix with color scale legend.

I don't get ur stretched ellipses problem, shouldnt it be a colored squred for each data point?

u can try log color scale if it is sparse. also plot the 12 classes separately to analyze if theres any inter-class differences.

Comments

0

plt.matshow also turned out to be a feasible solution. I could also plot a heatmap with colorbars and all that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.