Is there anyway to do a symmetrical log plot (i.e., in both positive and negative directions), through Plots.jl
? I know matplotlib
has this functionality.
I didn’t find a way to do it directly with Plots.jl
, but I implemented something for my research that could be useful for you:
using Plots, LaTeXStrings
pgfplots()
n = 20
x = [i for i = 0:n]
y = [(-1)^i * 10^(-i/2+4) for i = 0:n]
function symlog(y, C)
return sign.(y) .* (log10.(1 .+ abs.(y) / (10^C)))
end
function invsymlog(y, C)
return sign.(y) .* 10^C .* (-1 .+ 10 .^ (abs.(y)))
end
C = ceil(minimum(log10.(abs.(y))))
z = symlog(y, C)
plot(x, z, lw=1, xlabel=L"k", ylabel=L"\mathcal{E}(x,y)", label="", legend=:best)
savefig("metric.tex")
open("figure.tex", "w") do io
println(io, "\\documentclass[tikz]{standalone}")
println(io, "\\usepackage{pgfplots}")
println(io, "\\usepackage{tikz}")
println(io, "\\pgfplotsset{compat=1.15}")
println(io, "\\begin{document}")
end
run(`bash -c 'cat metric.tex >> figure.tex'`)
open("figure.tex", "a") do io
println(io, "\\end{document}")
end
run(`pdflatex figure.tex`)
After that you only need to change ytick
and yticklabels
in the latex file.
yticklabels = {$-10$,$-5$,$0$,$5$,$10$}
becomes {$-10^4$,$-10^(-1)$,$-10^(-6)$,$10^(-6)$,$10^(-1)$,$10^4$}
.
With C=-6
in that case, -10
is replaced by -10^(10+C)
and 5
is replaced by 10^(5+C)
.
Now ytick
needs to be changed to take the value of symlog
applied to {-10^4,-10^{-1},-10^{-6},10^{-6},10^{-1},10^4}
which means that ytick = {-10.0,-5.0,0.0,5.0,10.0}
becomes ytick ={ -10.00000000004343,-5.000004342923105,-0.3010299956639813,0.3010299956639813,5.000004342923105,10.00000000004343}
.
With sed
commands it should be possible to automate this last part of the process.
Final figure: