# How to customize display and plot of a sparse matrix?

**URL:** <https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099>\
**Category:** General Usage\
**Created:** [September 21, 2023, 8:51am UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099 "2023-09-21T08:51:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leo\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leo_i/32/27262_2.png) [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Post date:** [September 21, 2023, 8:51am UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099/1 "2023-09-21T08:51:00Z")

</div>

Hey, I have 2 questions regarding displaying SparseArrays.

**(1)** When using `SparseArrays` the code

```julia
n=15; x=sprand(Float16,n,n,0.3); x.nzval .=1; display(x)

```

produces

```julia
15×15 SparseMatrixCSC{Float16, Int64} with 67 stored entries:
  ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ 1.0
  ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ 1.0 1.0
 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ 1.0 ⋅ 
 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ 
  ⋅ 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ 1.0
  ⋅ ⋅ 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 1.0 1.0 ⋅ 
 1.0 ⋅ 1.0 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 1.0 1.0 1.0 1.0 1.0 ⋅ 
  ⋅ 1.0 1.0 1.0 ⋅ ⋅ ⋅ 1.0 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 
  ⋅ 1.0 ⋅ ⋅ 1.0 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 1.0
  ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ 1.0 ⋅ 1.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ 1.0
  ⋅ ⋅ ⋅ ⋅ 1.0 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ 1.0 1.0 1.0 ⋅ ⋅ 

```

However, changing to `n=16` gives

```julia
16×16 SparseMatrixCSC{Float16, Int64} with 82 stored entries:
⎡⢹⠤⢀⠥⠉⠎⠈⣱⎤
⎢⡘⠅⠍⠂⠀⠑⠣⠠⎥
⎢⡤⠊⡦⣀⢘⠴⣠⠖⎥
⎣⣐⠹⠈⠐⡏⡅⢀⠛⎦

```

When working on a bigger 4K screen, this is unsatisfactory. How can I customize the display, so that I set the max number of rows/columns to either some specific numbers, or to screen/terminal height/width?

I do not wish to convert to a dense matrix, since the `.` are useful to immediately see the zeros. With dense matrices, the width and height of the screen/terminal is correctly detected, and truncation is done only after that width/height is crossed.

**(2)** How could I display sparse arrays larger than the threshold? In Mathematica, this is doable (though it is viewed as a dense matrix):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/0/70f59f90237d0d349d220db07aa93dba841a699c.png)  
At the bottom, you can see the slider, and you can use it to inspect the whole matrix.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [September 21, 2023, 3:29pm UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099/2 "2023-09-21T15:29:35Z")

</div>

The value of `16` is hard coded:

```julia
function Base.print_array(io::IO, S::AbstractSparseMatrixCSCInclAdjointAndTranspose)
    if max(size(S)...) < 16
        Base.print_matrix(io, S)
    else
        _show_with_braille_patterns(io, S)
    end
end

```

I.e you’d need to call `print_matrix` directly. Something like

```julia
julia> n=18; x=sprand(Float16,n,n,0.3); x.nzval .=1;
julia> io = IOContext(stdout, :typeinfo => eltype(x), :compact=>true, :limit=>true)
IOContext(Base.TTY(Base.Libc.WindowsRawSocket(0x000000000000023c) open, 0 bytes waiting))

julia> Base.print_matrix(io, x)
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ 1.0 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ 1.0 ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ 1.0 1.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0
  ⋅ ⋅ 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 1.0 1.0 1.0 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ 1.0
 1.0 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅
  ⋅ ⋅ 1.0 1.0 1.0 ⋅ ⋅ ⋅ 1.0 1.0 ⋅ ⋅ 1.0 1.0 ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ 1.0
  ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 1.0 ⋅ ⋅ 1.0
 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ 1.0 1.0 1.0 1.0 1.0 ⋅ ⋅ 1.0 1.0 ⋅ ⋅ ⋅
 1.0 ⋅ 1.0 ⋅ 1.0 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 1.0
  ⋅ ⋅ 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ 1.0 1.0 ⋅ 1.0 1.0 1.0 ⋅ 1.0 1.0
 1.0 ⋅ ⋅ ⋅ ⋅ 1.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ 1.0 ⋅ 1.0 1.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅
 1.0 1.0 ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅
  ⋅ ⋅ ⋅ 1.0 ⋅ 1.0 1.0 ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 1.0 1.0 ⋅ 1.0 ⋅ ⋅ ⋅

```

Would be helpful if that cutoff could beoverridden in an `IOContext`.

---

<div class="post-metadata">

**Author:** ![Leo\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leo_i/32/27262_2.png) [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Post date:** [September 21, 2023, 4:26pm UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099/3 "2023-09-21T16:26:55Z")

</div>

> [@skleinbo](#):
>
> `_show_with_braille_patterns`

That looks much better! And how could I also use `_show_with_braille_patterns`? I’m getting an error:

```julia
n=60; x=sprand(Float16,n,n,0.3); x.nzval .=1;   
io = IOContext(stdout, :typeinfo => eltype(x), :compact=>true, :limit=>true);   
_show_with_braille_patterns(io, x)

ERROR: UndefVarError: `_show_with_braille_patterns` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[22]:1

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 21, 2023, 5:00pm UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099/5 "2023-09-21T17:00:36Z")

</div>

> [@Leo\_I](#):
>
> And how could I also use `_show_with_braille_patterns`? I’m getting an error:

It’s an internal function in the SparseArrays module:

```julia
julia> SparseArrays._show_with_braille_patterns(stdout, sprand(30,30,0.1))
⎡⠀⠀⠂⠈⠐⠀⠤⡠⠀⡠⠀⡀⠀⡄⠀⎤
⎢⡁⠈⠄⠀⢀⡣⠀⠀⢀⠄⠀⠀⠬⠂⠀⎥
⎢⡰⠢⠀⠀⡀⢠⠁⠀⡀⠑⠀⠀⠄⡂⢀⎥
⎢⠀⠑⠀⠈⠒⠴⠀⠉⠀⡈⠈⠀⠠⠠⠀⎥
⎢⡀⠄⠐⢀⠀⠀⠆⠠⠀⠀⠁⠄⢀⠀⠀⎥
⎢⠐⠀⠜⠀⠀⠐⠀⠀⠀⢄⠁⠀⠀⠀⡐⎥
⎢⢌⠀⠀⠠⡀⠀⠀⠀⠐⢀⠁⠀⡠⠀⠅⎥
⎣⠀⠂⠑⠈⠀⠀⠀⠀⠀⠊⠁⠀⠀⠀⠀⎦

```

---

<div class="post-metadata">

**Author:** ![Leo\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leo_i/32/27262_2.png) [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Post date:** [September 21, 2023, 7:27pm UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099/6 "2023-09-21T19:27:40Z")

</div>

Thank you, now displaying in REPL works as I wanted. But in jupyter, It does not:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/1/c1cb903ce516bce2ba98687e8e6d3943127af707.png)

Is there a way to display a specific number of rows and columns and have sliders, like in Mathematica?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 21, 2023, 7:42pm UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099/7 "2023-09-21T19:42:40Z")

</div>

> [@Leo\_I](#):
>
> But in jupyter, It does not:

That’s because the display size is taken from `ENV["LINES"]` AND `ENV["COLUMNS"]` in Jupyter (IJulia doesn’t know the size of your browser window). If you want to print everything, just use `:limit=>false`. Or simply do:

```julia
Base.print_matrix(stdout, x)

```

with no `IOContext`.

---

<div class="post-metadata">

**Author:** ![Leo\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leo_i/32/27262_2.png) [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Post date:** [September 21, 2023, 8:28pm UTC](https://discourse.julialang.org/t/how-to-customize-display-and-plot-of-a-sparse-matrix/104099/8 "2023-09-21T20:28:31Z")

</div>

Thank you for your help! @stevengj @skleinbo
