Python: Plot a sparse matrix
<p>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.</p>

<p>Attempts:</p>

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

<p>Bonus:</p>

<ul>
<li>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).</li>
<li>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. </li>
</ul>

<p>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. </p>

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

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

python matplotlib