Skip to content
Advertisement

Trying to make a graph from a sparse matrix: not enough values to unpack (expected 2, got 0)

So I’m trying to make a graph with squares that are colored according to probability densities stored in the 7×7 matrix ‘nprob’.

JavaScript

I get the following error:

JavaScript

To be honest, I get this error a lot, and I usually just rejuggle things until I get one I understand better, so it’s probably about time to learn what it means. What isn’t clear? I want it to graph the probability density at the 49 specified points on the grid.

Advertisement

Answer

Make a sample sparse matrix (you could have provided one :( ):

JavaScript

Note what your indexing does – not much – it’s still as csr matrix:

JavaScript

Now your plot:

JavaScript

But what if we plot the dense version of that matrix:

JavaScript

It works.

plt doesn’t know anything (special) about sparse matrices. I suspect it is just doing:

JavaScript

That’s a 0d object dtype array, not a 2d array that the plot function expects.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement