Saving unicodeplots() to txt with color

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):

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

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

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

and just

txt("/tmp/plot1.txt")

if you want to save the current plot.

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

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).

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.

The cited issue (posted the previous day) is:

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> 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.