# How to display a sparse array on the REPL?

**URL:** <https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [April 27, 2024, 1:33pm UTC](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565 "2024-04-27T13:33:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 27, 2024, 1:33pm UTC](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/1 "2024-04-27T13:33:25Z")

</div>

If I construct a sparse matrix on the REPL, it can directly display the distribution of non-zero elements of the matrix:

```julia
julia> using SparseArrays

julia> sparse(round.(rand(5,5)))
5×5 SparseMatrixCSC{Float64, Int64} with 12 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

```

Now I want to construct a sparse matrix in a `.jl` script and display it when run the script, for example:

```julia
using SparseArrays
a = sparse(round.(rand(5,5)))
println("The sparse matrix is:")
# Print the non-zero elements' distribution on the REPL. How to write?
println("Over.")

```

How to write?

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [April 27, 2024, 1:38pm UTC](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/2 "2024-04-27T13:38:30Z")

</div>

The REPL uses basically just uses the `show` method to display output. So you can just use `show(x)` in your script

---

<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:** [April 27, 2024, 1:42pm UTC](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/3 "2024-04-27T13:42:53Z")

</div>

> [@WuSiren](#):
>
> `# Print the non-zero elements' distribution on the REPL. How to write?`

The REPL [calls `display(a)`, which calls `show(stdout, , "text/plain", a)`](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing) (more precisely, it wraps `stdout` in an [`IOContext`](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext) that limits output.

So, in your script you can either call `display(a)` or call `show(io, "text/plain", a)` if you want to output to a particular `io` stream.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 27, 2024, 1:44pm UTC](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/4 "2024-04-27T13:44:32Z")

</div>

But this can’t display the figure:

```julia
using SparseArrays
a = sparse(round.(rand(5,5)))
println("The sparse matrix is:")
show(a)
println("Over.")

```

```julia
The sparse matrix is:
sparse([1, 2, 4, 5, 3, 4, 2, 3, 1, 2, 3, 4, 1], [1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5], [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], 5, 5)Over.

```

---

<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:** [April 27, 2024, 1:44pm UTC](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/5 "2024-04-27T13:44:51Z")

</div>

> [@screw\_dog](#):
>
> The REPL uses basically just uses the `show` method to display output. So you can just use `show(x)` in your script

No, this is wrong. The 1-argument `show(x)` (which is also called by `print(x)` for most types) calls the 2-argument `show(stdout, x)`. For matrices, this is less verbose than the 3-argument `show(stdout, "text/plain", x)` called by the REPL’s `display`.

Summary:

- [`display(x)`](https://docs.julialang.org/en/v1/base/io-network/#Base.Multimedia.display) tells the current environment to display `x` in whatever way it thinks best. This might be a graphical display in something like a Jupyter notebook. By _default_ (e.g. in scripts or in the text REPL), it calls `show(io, "text/plain", x)` or equivalently `show(io, MIME"text/plain"(), x)` for an appropriate `io` stream. (In the REPL, `io` is an `IOContext` wrapper around `stdout`.) The REPL uses `display` to output the result of an evaluated expression.
- The 3-argument [`show(io, ::MIME"text/plain", x)`](https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple%7BIO,%20Any,%20Any%7D) method is verbose pretty-printing of `x`. By _default_ (if no verbose method is defined for `typeof(x)`), it calls the 2-argument `show(io, x)`.
- The 2-argument [`show(io, x)`](https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple%7BIO,%20Any%7D) is the default simple text representation of `x`. It is what is used by [`repr(x)`](https://docs.julialang.org/en/v1/base/strings/#Base.repr-Tuple%7BAny%7D), and is typically the format you might use to _input_ `x` into Julia. The 1-argument `show(x)` calls `show(stdout, x)`.
- [`print(x)`](https://docs.julialang.org/en/v1/base/io-network/#Base.print) calls `print(stdout, x)`, which by _default_ calls `show(stdout, x)`. It mainly differs from `show` when `x` is a string, where `print` outputs the raw text but `show` outputs an escaped string enclosed in quotation marks.

_Update May 2024:_ a version of this summary should be added to the manual ([julia#54547](https://github.com/JuliaLang/julia/pull/54547)).

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 27, 2024, 1:49pm UTC](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/6 "2024-04-27T13:49:07Z")

</div>

> [@stevengj](#):
>
> show(stdout, “text/plain”, x)

Nice! Professional! Thanks a lot! 🤝 🤝
