Gadfly.jl icon indicating copy to clipboard operation
Gadfly.jl copied to clipboard

Format option for log scale.

Open jonathanBieler opened this issue 5 years ago • 3 comments

Sometimes I'd like my axis to be labelled 1, 10, 100 instead of 10^0, 10^1, 10^2. I thought the format option should do that (Scale.x_log10(format=:plain)) but apparently that's not the case. Actually setting the format to scientific labels things 10^1x10^0 which is not very useful.

I know that I can use the labels option to format my labels, but we could probably make better use of the format option here, no ?

jonathanBieler avatar Oct 09 '19 14:10 jonathanBieler

As you note, the difference between format and labels is:

  • format changes the notation of numbers in the label
  • labels is used to change the label format e.g. 10² or 100 or e4.60517

The notation of numbers is changed via the function showoff e.g. Gadfly.showoff([10.0], :scientific), which is then passed to the labelling function.

To change this, some design discussion would be needed, so please outline your ideas about notation and labelling.

Mattriks avatar Oct 09 '19 17:10 Mattriks

I would just do something like this for log10_formatter :

function log10_formatter(xs::AbstractArray, format=:scientific) 
    if format == :plain
        return [@sprintf("%s", x) for x in showoff((x -> 10^x).(xs), format)]
    else
        return [@sprintf("10<sup>%s</sup>", x) for x in showoff(xs, :plain)]
    end
end

Which seems to work fine except I'm getting to many decimals displayed in the plain format.

Basically make the scientific format the default for log scales (10^x is the scientific format after all).

jonathanBieler avatar Oct 16 '19 09:10 jonathanBieler

+10^2 for making it easy to display 100! :)

i like @jonathanBieler 's approach.

lots of decimals are displayed by showoff because it is being called with an extended range of tick marks, not just the ones displayed. put @info xs into the code in the preceeding comment to demonstrate. one can suppress this behavior by setting extend_tick to false here. i believe the reason for the extension is to facilitate panning and zooming in SVGJS. the code which decides what extra tick marks to add seems to presume a linear scale. not sure how best to change it for log scales.

bjarthur avatar Oct 20 '19 16:10 bjarthur