Search code examples
plotlyjuliajulia-plots

Plots.jl: Change formating of logscale ticks to normal formatting


Consider the following plot

using Plots
pyplot()

radix_range_xticks = -4:4
radix_range = LinRange(-4, 4, 100)
xs = 2.0 .^ radix_range_xticks
pf = 50.0
bw = 25.0
roofline(x, bw, pf) = min(bw*x, pf)
ys = roofline.(xs, bw, pf)
p = plot(xs, ys; xscale=:log2, yscale=:log10)
xticks!(p, xs)

This results in a plot that looks like this.

View of the plot

The x-ticks and y-ticks both are formatted as exponentials. Adding the keyword argument xformatter=:scientific does not help, as it just changes the radix instead of the whole number.

With xformatter=:scientific

So, how can I format the ticks in e.g. scientific notation? I would like to get rid of the base^exponent notation altogether.

I also tried passing an iterable of Strings to xticks!, but it does not dispatch on it.

I tried to look here in the documentation, but could not anything else that might help.

Crossposted on the Julia lang discourse here


Solution

  • Passing only an iterable of strings would not work, the plot also needs information about where to place those strings on the x-axis. The following does the job.

    xticks!(p, xs, string.(xs))
    

    Replace the last argument with whatever formatting is required for the ticks.