# Saving unicodeplots() to txt with color

**URL:** <https://discourse.julialang.org/t/saving-unicodeplots-to-txt-with-color/60069>\
**Category:** Visualization\
**Tags:** question, hpc, plots, statsplots\
**Created:** [April 26, 2021, 11:02pm UTC](https://discourse.julialang.org/t/saving-unicodeplots-to-txt-with-color/60069 "2021-04-26T23:02:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [April 26, 2021, 11:02pm UTC](https://discourse.julialang.org/t/saving-unicodeplots-to-txt-with-color/60069/1 "2021-04-26T23:02:41Z")

</div>

This is the solution I didn’t know I needed for quick and dirty hpc plotting.

However, they only show up monochrome in `#SBATCH --output`.

Therefore, I tried the following to explicitly save them and neither approach worked (please see inline comments):

```julia
using MCMCChains,UnicodePlots,StatsPlots

niter = 4000
nparams = 3
nchains = 2
tchain = Chains(rand(niter, nparams, nchains), ["a", "b", "c"], Dict(:internals => ["c"]))

unicodeplots()
display(density(tchain,colordim=:parameter))

### gives a file containing only the line: `Plot{Plots.UnicodePlotsBackend() n=4}`
open("/tmp/plot1.txt","w") do io
    print(io,density(tchain,colordim=:parameter))
end

### gives an empty file
open("/tmp/plot2.txt","w") do io
    display(density(tchain,colordim=:parameter))
end

```

---

<div class="post-metadata">

**Author:** ![devmotion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devmotion/32/9201_2.png) [@devmotion](https://discourse.julialang.org/u/devmotion)\
**Post date:** [April 26, 2021, 11:31pm UTC](https://discourse.julialang.org/t/saving-unicodeplots-to-txt-with-color/60069/2 "2021-04-26T23:31:10Z")

</div>

Just a wild guess after quickly looking at [Plots.jl/unicodeplots.jl at master · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/blob/master/src/backends/unicodeplots.jl) and [https://github.com/JuliaPlots/Plots.jl/blob/master/src/output.jl:](https://github.com/JuliaPlots/Plots.jl/blob/master/src/output.jl:) Maybe you can use

```julia
txt(density(tchain, colordim=:parameter), "/tmp/plot1.txt")

```

and just

```julia
txt("/tmp/plot1.txt")

```

if you want to save the current plot.

---

<div class="post-metadata">

**Author:** ![devmotion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devmotion/32/9201_2.png) [@devmotion](https://discourse.julialang.org/u/devmotion)\
**Post date:** [April 26, 2021, 11:37pm UTC](https://discourse.julialang.org/t/saving-unicodeplots-to-txt-with-color/60069/3 "2021-04-26T23:37:40Z")

</div>

Another wild shot: Maybe it is even possible to plot with colors if you use an `IOContext`, similar to

```julia
open("/tmp/plot1.txt", "w") do io
    show(IOContext(io, :color => true), MIME("text/plain"), plt)
end

```

where `plt` is the plot that you want to save, i.e., `density(tchain, colordim=:parameter)` or `Plots.current()` (if you want to save the current figure).

---

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [April 27, 2021, 9:34pm UTC](https://discourse.julialang.org/t/saving-unicodeplots-to-txt-with-color/60069/4 "2021-04-27T21:34:59Z")

</div>

Thanks, @devmotion.

I went back to Plots.jl (since StatsPlots.jl provides compatible recipes) and found [a PR addressing Plots.savefig() autodetection of .txt extension which was merged on 02-12-2020](https://github.com/JuliaPlots/Plots.jl/pull/2396).

The [cited issue](https://github.com/JuliaPlots/Plots.jl/issues/2395#issue-563371610) (posted the previous day) is:

```julia
using Plots

unicodeplots()

x = 1:0.01:10
y1 = sin.(x)
y2 = cos.(x)

p = Plots.plot(x, y1)
Plots.plot!(p, x, y2)

Plots.savefig(p, "/tmp/p.txt")
read(`less /tmp/p.txt`,String)

```

Note that this issue was closed on 2-17-2020 (resolved by the PR above) but this code still does not work according to the following output:

```julia
julia> Plots.plot!(p, x, y2)
                       ┌────────────────────────────────────────────────────────────┐   
     1.059999635371616 │⠀⠀⠀⣀⡤⠤⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠤⣄⠀⠀⠀⠀⠀⢀⣠⠤⠤⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ y1
                       │⠀⢀⡜⠁⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠀⠀⠀⠈⠳⡄⠀⠀⣠⠋⠀⠀⠀⠈⠳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ y2
                       │⠀⠈⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠃⠀⠀⠀⠀⠀⠀⠙⡄⡴⠁⠀⠀⠀⠀⠀⠀⠹⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│   
                       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠃⠀⠀⠀⠀⠀⠀⠀⠀⡼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠹⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀│   
                       │⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠀⣰⠁⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠹⡀⠀⠀⠀⠀⠀⠀⠀⠀│   
                       │⠀⠈⣆⠀⠀⠀⠀⠀⠀⠀⠀⠘⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠏⠀⠀⠀⠀⠀⠀⠀⠀⢠⠇⠀⠀⢱⡀⠀⠀⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⠀⠀⠀⠀│   
                       │⠀⠀⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⢹⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠀⠀⠀⠀⠀⢀⡎⠀⠀⠀⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠈⢇⠀⠀⠀⠀⠀⠀⠀│   
                       │⠀⠀⠀⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⠁⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠀⠀⠀⠀⠘⡆⠀⠀⠀⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀│   
                       │⠀⠀⠀⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⢰⠃⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀│   
                       │⠀⠀⠀⠀⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠀⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⢇⠀⠀⠀⠀⠀│   
                       │⠉⠉⠉⠉⠉⢻⠉⠉⠉⠉⠉⠉⠉⠉⠉⢫⠉⠉⠉⠉⠉⠉⠉⠉⣹⠉⠉⠉⠉⠉⠉⠉⠉⠉⡽⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⡏⠉⠉⠉⠉⠉⠉⠉⠉⠙⡍⠉⠉⠉⠉│   
                       │⠀⠀⠀⠀⠀⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀│   
                       │⠀⠀⠀⠀⠀⠀⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠀⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⢧⠀⠀⠀│   
                       │⠀⠀⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⡸⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣆⠀⠀⠀⠀⠀⠀⠀⠀⠘⡆⠀⠀│   
                       │⠀⠀⠀⠀⠀⠀⠀⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠀⣰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠹⡀⠀│   
                       │⠀⠀⠀⠀⠀⠀⠀⠀⠈⣆⠀⠀⠀⠀⠀⠀⠀⠀⠘⡆⢠⠏⠀⠀⠀⠀⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢱⡀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀│   
                       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣆⠀⠀⠀⠀⠀⠀⠀⠀⢘⡎⠀⠀⠀⠀⠀⠀⠀⠀⢠⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀│   
                       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣆⠀⠀⠀⠀⠀⠀⢠⠎⠙⣄⠀⠀⠀⠀⠀⠀⢠⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢳⡀⠀⠀⠀⠀⠀⠀⡀⠀│   
                       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢦⡀⠀⠀⠀⣠⠋⠀⠀⠘⢦⡀⠀⠀⢀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⣄⠀⠀⠀⢀⡜⠁⠀│   
   -1.0599986841673208 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠒⠒⠋⠁⠀⠀⠀⠀⠀⠙⠒⠒⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠓⠒⠚⠉⠀⠀⠀│   
                       └────────────────────────────────────────────────────────────┘   
                       0.73 10.27
julia> Plots.savefig(p, "/tmp/p.txt")

julia> read(`less /tmp/p.txt`,String)
"Plot{Plots.UnicodePlotsBackend() n=2}"

```

I think once this is resolved, the additional recipes in StatsPlots.jl will work as expected with `savefig()` under the unicoteplots backend.
