# Spy function for large sparse matrices

**URL:** <https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247>\
**Category:** New to Julia\
**Created:** [May 29, 2018, 6:49pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247 "2018-05-29T18:49:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 29, 2018, 6:49pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/1 "2018-05-29T18:49:06Z")

</div>

I tried

```julia
julia> using Plots
julia> spy(A)

```

for a rather large sparse matrix, and `spy` just hangs there for too long for it to be useful to me… MATLAB’s `spy` is quite fast in comparison. I’m guessing Julia’s `spy` plot is not binning the data? But how can I get that info since I can’t find the documentation:

```julia
help?> spy
search: spy spy! speye display Display displaysize displayable redisplay popdisplay isempty TextDisplay supertype pushdisplay

  No documentation found.

  Plots.spy is a Function.

  # 1 method for generic function "spy":
  spy(args...; kw...) in Plots at /Users/XXXXXX/.julia/v0.6/RecipesBase/src/RecipesBase.jl:367

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 29, 2018, 7:28pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/2 "2018-05-29T19:28:34Z")

</div>

> [@briochemc](#):
>
> What am I doing wrong?

Is it only slow the first time? That’s a known Plots.jl problem. If it’s more than just the first time, then it’s something else.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 29, 2018, 7:57pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/3 "2018-05-29T19:57:34Z")

</div>

It is that slow that I don’t know how long it is gonna take because I cannot afford to wait… In MATLAB, `spy` is a convenient way to check a sparsity pattern, and takes almost no time to plot. It seems to me `spy` should never be slow, right?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 29, 2018, 8:06pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/4 "2018-05-29T20:06:38Z")

</div>

```julia
using UnicodePlots
spy(A) 

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 29, 2018, 8:08pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/5 "2018-05-29T20:08:32Z")

</div>

> [@briochemc](#):
>
> It is that slow that I don’t know how long it is gonna take because I cannot afford to wait… In MATLAB, `spy` is a convenient way to check a sparsity pattern, and takes almost no time to plot. It seems to me `spy` should never be slow, right?

Plots.jl has a slow first plot time because that’s when it sets up the backend and this is orthogonal to `spy`. You still haven’t mentioned whether it’s just the first plot time or not.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 29, 2018, 8:59pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/6 "2018-05-29T20:59:49Z")

</div>

I’m sorry, it is not the first use of `spy`, but it is the first use on a matrix of that size.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [May 29, 2018, 9:01pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/7 "2018-05-29T21:01:45Z")

</div>

It also depends on the backend you are using, Gadlfy’s spy is very slow for example. Maybe try GR. Sometimes I just write a png, which is often faster that plotting.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 29, 2018, 9:33pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/8 "2018-05-29T21:33:34Z")

</div>

> ```julia
> using UnicodePlots
> spy(A) 
> 
> ```

Thank you, that seems to be a good solution 🙂

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 29, 2018, 9:39pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/9 "2018-05-29T21:39:57Z")

</div>

I am I am properly using the GR backend, but

```julia
julia> using Plots
julia> gr()
julia> spy(A)

```

did not work for me (still very slow, actually had to close the terminal window as CTRL-C could not stop the process).

---

<div class="post-metadata">

**Author:** ![Jorge\_Vieyra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_vieyra/32/6527_2.png) [@Jorge\_Vieyra](https://discourse.julialang.org/u/Jorge_Vieyra)\
**Post date:** [February 2, 2020, 7:09pm UTC](https://discourse.julialang.org/t/spy-function-for-large-sparse-matrices/11247/10 "2020-02-02T19:09:29Z")

</div>

Hmmm… this is more like a workaround. I have been using spy for UnicodePlots hoping that the spy from Plots would get fixed, but it still seems to be incredibly slow.
