Hi All,
Is there a way to plot a histogram in Plots.jl with a symlog scale on the x-axis? With matplotlib, there’s a symlog x-scale property. However, there doesn’t seem to be one in Plots.jl? Unless I’ve missed something?
using Plots
# Example data
data = rand(10_000)
# Plot histogram (transformed data)
plt = histogram(
data,
bins = 50,
xscale = :symlog10
)
with the error,
┌ Warning: scale symlog10 is unsupported with Plots.GRBackend().
│ Choose from: [:identity, :ln, :log10, :log2]
└ @ Plots ~/.julia/packages/Plots/8ZnR3/src/args.jl:1590
Bumping this thread to see if there’s an easy way to do this that I’ve missed.
Thanks in advance!
There’s no native way to do this in Plots, so a kludge is required.
using Plots
gr()
# Define the symlog transformation function
symlog10(x, thr=1.0) = sign(x) * log10(1 + abs(x)/thr)
inv_symlog10(x, thr=1.0) = sign(x) * thr * (10^abs(x) - 1)
x = range(-100, 100, length=1000)
y = x.^3
# Transform data
y_trans = symlog10.(y)
# Generate appropriate ticks
ticks = [-10^6, -10^4, -10^2, 0, 10^2, 10^4, 10^6]
tick_labels = [string(t) for t in ticks]
tick_positions = symlog10.(ticks)
plt = plot(x, y_trans, yticks = (tick_positions, tick_labels),
title = "Manual Symlog Transformation", ylabel = "Log Scale (Symmetric)")
plt = plot!(plt, size = (600, 400))
savefig(plt, "symlog_plot.png")
Makie, however, has a native way to do this.
using CairoMakie, Makie
fig = Figure()
ax = Axis(fig[1,1], yscale = Makie.Symlog10(1.0))
x = -100:100
lines!(ax, x, sin.(x / 10), linewidth = 2)
fig
2 Likes
Thanks for the prompt reply, @technocrat !
Do you know if this is possible with plotting a histogram with a symmetric log x-axis?
Not technically a histogram, but a barplot. The y-axis of a histogram is counts.
using CairoMakie
fig = Figure()
ax = Axis(fig[1,1], yscale = Makie.Symlog10(1.0))
x = 1:20
y = [-500, 3, -80, 1200, -2, 450, -9000, 7, -150, 300,
-1, 850, -45, 2000, 6, -700, 12, -3500, 99, -18]
barplot!(ax, x, y)
fig
1 Like