Plots.jl: feature request: spy function for matrices

Created on 2 Jun 2017  路  13Comments  路  Source: JuliaPlots/Plots.jl

There was a feature request for a spy function to graphically represent matrices (especially useful for sparse matrices): issue 40. It was closed a while back. Is that spy function available? Is there a preferred alternative? Thanks.

All 13 comments

There is a spy recipe, but something seems amiss with it now, as it does not produce any markers in the plot. It should be as easy as using Plots; spy(my_matrix)

I think the default marker size (1) is too small in the recipe. Try for example:

using Plots

n = 20
mat = zeros(Int, n, n)
for i in 1:n
    for j in i:n
        mat[i, j] = i * j
    end
end

# barely visible
spy(mat)

# better
spy(mat, markersize = 6)

For bigger ns a smaller markersize is probably more useful, but we could default to something slightly higher or print an info to possibly increase the markersize.

Also, in my example the colorbar labels ar confusing on GR. They work correctly on PyPlot.

I guess a small default value is reasonable, because spy is probably frequently used to view large sparse matrices. I think I will submit a PR with an info about the default markersize in the spy recipe. What do you say @mkborregaard ?

Thanks Michael and Daniel. I got it to work!

I think I'd been reading random threads rather than proper documentation, so I didn't know about markersize. Relatedly I stumbled upon this discussion issue 454 and became aware of UnicodePlots.spy. In a way that suited my specific purpose better because the result was instantly spilled into the console and because I did not need the heatmap style coloring.

But if I ever needed to export an image, I think the heatmap would be more presentable, as it were. Is it possible to remove the color scale and force a single color, say red?

Thanks again.

# This was my use case (more or less):
A = spdiagm((ones(Float64, 500), ones(Float64, 499), ones(Float64, 499), ones(Float64, 450), ones(Float64, 450)),(0, 1, -1, 50, -50))
using Plots: spy
spy(A, markersize = 1)

using UnicodePlots
UnicodePlots.spy(A)

tmp

tmp2

spy(A, markersize = 1, legend = nothing) produces a plot without colors and without colorbar.

Nice to know about legend = nothing!

I'm getting a lot of InitError: MethodError: no method matching rawtype(::Type{LastMain.LastMain.LastMain.LastMain.LastMain.LastMain.FixedPointNumbers.FixedPoint{UInt8,8}}) and the occasional Julia has stopped: null, SIGSEGV . I can't find a pattern, seems random. I copy-paste your code above, precede it with workspace(), sometimes it works, sometimes it crashes.

workspace() crashes are a well-known bug. You should never (have to) call workspace().

I guess a small default value is reasonable, because spy is probably frequently used to view large sparse matrices.

Agreed. We should deactivate the grid, though, that just confuses things (that's what got me confused for a moment, sorry). Maybe we could also deactivate the color bar when all values are identical, though I guess that happens so rarely it doesn't matter.

Alright, I will be offline for a few days :-)

@ptoche let me just add one thing :-) I came from R, where there is no useful documentation and you learn by googling and stackoverflow. Great was my surprise when I came to julia and realized that reading the documentation (also the julia docs themselves) is the path to success! I'd highly recommend reading every word of http://juliaplots.github.io/ - it's all good stuff.

Thanks Michael, will read the docs!

I didn't see the spy recipe documented under https://juliaplots.github.io/recipes/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kersulis picture kersulis  路  5Comments

Cody-G picture Cody-G  路  3Comments

cortner picture cortner  路  4Comments

Krastanov picture Krastanov  路  3Comments

apalugniok picture apalugniok  路  3Comments