jheinen / GR.jl

Plotting for Julia based on GR, a framework for visualisation applications
Other
353 stars 76 forks source link

Extra space in exponential notation #279

Open miguelfm opened 4 years ago

miguelfm commented 4 years ago

Positive exponents look fine in exponential notation, but in the negative ones an extra blank space appears after the minus sign, ie. "8e- 01" instead of "8e-01". For instance:

using GR
x=[0.001,0.1,1]; y=x
xticklabels((x)->Base.Printf.@sprintf("%1.0e",x))
plot(x,y)

It is funny that using the gr backend within Plots this issue shows only in logarithmic scales. This strange behavior doesn't happen with other backends like pyplot.

using Plots
using Formatting
gr()
x=[0.001,0.1,1]; y=x
scatter(x,y,
    xaxis=:log10,
    xformatter=x->sprintf1("%1.0e",x),
    yformatter=y->sprintf1("%1.0e",y))

While the y axis is printed fine, in the x one (logarithmic scale) appears an extra blank space after the "e-".

heliosdrm commented 4 years ago

Due to how GR manages text strings, you should escape the minus sign, e.g. like this:

using GR
x=[0.001,0.1,1]; y=x

function prettyexp(x)
    s = Base.Printf.@sprintf("%0.1e", x)
    replace(s, "-"=>"\\-")
end

xticklabels(prettyexp)
plot(x,y)
miguelfm commented 4 years ago

Thank you very much. It is working well now. To use within plots and other backends I have to add the following code:

    if occursin("GR",string(backend()))
        s=replace(s, "-"=>"\\-") # Needed to Escape "-" in GR
    end

Anyway, having to add this code just to "support" the minus sign in exponential notation is quite ugly. Is there any other way to fix this in the GR source code?

heliosdrm commented 4 years ago

It is not just for the minus sign, but for other symbols that might be confounded with LaTeX-like math codes, e.g. _ (subscript), ^ (superscript), / (fraction), different types of brackets, etc. This applies to all texts: in ticks, axis labels, titles, legends, etc.

(What I don't know is what is the full set of symbols that have such formatting meaning. I just found them by trial and error.)

miguelfm commented 4 years ago

So, it is an actual issue working with common used mathematical symbols in GR. I didn't know that. Thank you anyway!

heliosdrm commented 4 years ago

I think I found where the math symbols are defined:

https://github.com/sciapp/gr/blob/2692d04fb4b286e17a34bbed12dcd3b151d00d67/lib/gr/text.c#L245

miguelfm commented 4 years ago

It has to be something more. The issue happens to the "-" symbol but, for instance, there is no problem with "+".

jheinen commented 4 years ago

I would suggest to replace the '-' (minus) sign by an '–' (emdash) internally (in GR). What's your opinion?

heliosdrm commented 4 years ago

Maybe the specific issue of extra space with the minus thing is this (in the GR framework, not in the Julia package):

https://github.com/sciapp/gr/blob/2692d04fb4b286e17a34bbed12dcd3b151d00d67/lib/gr/text.c#L761-L763

case Minus:
  /* Minus.len < Plus.len => mLen = pLen */
  len = textwidth("+", font, prec);

There the space reserved for the - sign is equated to the space taken by the + sign, even though the symbol may be smaller.

About using the emdash instead of the minus sign, I don't have any special preference.

jheinen commented 4 years ago

I'm afraid I can't remember what caused this hack. But since it is "commented", there must have been a reason ... We should check it.

miguelfm commented 4 years ago

I would suggest to replace the '-' (minus) sign by an '–' (emdash) internally (in GR). What's your opinion? In my system replacing by emdash s=replace(s,"e-0"=>"e–") shows "â" instead of the minus character :(.

miguelfm commented 4 years ago

In case that this could be helpful, the same issue in Plots was already solved here https://github.com/JuliaPlots/Plots.jl/pull/2317 .

miguelfm commented 4 years ago

Due to how GR manages text strings, you should escape the minus sign, e.g. like this:

using GR
x=[0.001,0.1,1]; y=x

function prettyexp(x)
    s = Base.Printf.@sprintf("%0.1e", x)
    replace(s, "-"=>"\\-")
end

xticklabels(prettyexp)
plot(x,y)

I have just tested this example and doesn't work exporting plot as pdf (i.e. with: savefig("test.pdf")) test test.pdf