Merge pull request #1249 from apalugniok/pgfplots-scaled-ticks

WIP: Fix scientific formatter for PGFPlots and GR. Fixes #1241
This commit is contained in:
Josef Heinen 2018-01-10 19:05:13 +01:00 committed by GitHub
commit 4757bd89c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 2 deletions

View File

@ -222,6 +222,13 @@ function gr_polaraxes(rmin::Real, rmax::Real, sp::Subplot)
sinf = sind.(a)
cosf = cosd.(a)
rtick_values, rtick_labels = get_ticks(yaxis)
rtick_labels = if yaxis[:formatter] == :scientific && yaxis[:ticks] == :auto
rtick_labels = string.(convert_sci_unicode.(rtick_labels),"\\ ")
# unicode × messes up superscript alignment so add space to superscript
replace.(rtick_labels, "{", "{ ")
else
rtick_labels
end
#draw angular grid
if xaxis[:grid]
@ -882,7 +889,14 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
xi, yi = GR.wctondc(cv, sp[:framestyle] == :origin ? 0 : xor(flip, mirror) ? ymax : ymin)
# @show cv dv ymin xi yi flip mirror (flip $ mirror)
# ensure correct dispatch in gr_text for automatic log ticks
dv = xaxis[:scale] in (:ln, :log10, :log2) && xaxis[:ticks] == :auto ? string(dv, "\\ ") : string(dv)
dv = if xaxis[:scale] in (:ln, :log10, :log2) && xaxis[:ticks] == :auto
string(dv, "\\ ")
elseif xaxis[:formatter] == :scientific && xaxis[:ticks] == :auto
# unicode × messes up superscript alignment so add space to superscript
string(replace(convert_sci_unicode(dv), "{", "{ "), "\\ ")
else
string(dv)
end
gr_text(xi, yi + (mirror ? 1 : -1) * 5e-3 * (xaxis[:tick_direction] == :out ? 1.5 : 1.0), dv)
end
end
@ -895,7 +909,14 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
xi, yi = GR.wctondc(sp[:framestyle] == :origin ? 0 : xor(flip, mirror) ? xmax : xmin, cv)
# @show cv dv xmin xi yi
# ensure correct dispatch in gr_text for automatic log ticks
dv = yaxis[:scale] in (:ln, :log10, :log2) && yaxis[:ticks] == :auto ? string(dv, "\\ ") : string(dv)
dv = if yaxis[:scale] in (:ln, :log10, :log2) && yaxis[:ticks] == :auto
string(dv, "\\ ")
elseif yaxis[:formatter] == :scientific && yaxis[:ticks] == :auto
# unicode × messes up superscript alignment so add space to superscript
string(replace(convert_sci_unicode(dv), "{", "{ "), "\\ ")
else
string(dv)
end
gr_text(xi + (mirror ? 1 : -1) * 1e-2 * (yaxis[:tick_direction] == :out ? 1.5 : 1.0), yi, dv)
end
end

View File

@ -272,6 +272,9 @@ function pgf_axis(sp::Subplot, letter)
style = []
kw = KW()
# turn off scaled ticks
push!(style, "scaled $(letter) ticks = false")
# set to supported framestyle
framestyle = pgf_framestyle(sp[:framestyle])
@ -326,6 +329,7 @@ function pgf_axis(sp::Subplot, letter)
push!(style, string(letter, "ticklabels = {\$", join(tick_labels,"\$,\$"), "\$}"))
elseif axis[:showaxis]
tick_labels = ispolar(sp) && letter == :x ? [ticks[2][3:end]..., "0", "45"] : ticks[2]
tick_labels = axis[:formatter] == :scientific ? string.("\$", convert_sci_unicode.(tick_labels), "\$") : tick_labels
push!(style, string(letter, "ticklabels = {", join(tick_labels,","), "}"))
else
push!(style, string(letter, "ticklabels = {}"))
@ -497,6 +501,7 @@ end
function _display(plt::Plot{PGFPlotsBackend})
# prepare the object
PGFPlots.pushPGFPlotsPreamble("\\usepackage{fontspec}")
pgfplt = PGFPlots.plot(plt.o)
# save an svg

View File

@ -1048,3 +1048,28 @@ guidefont(ax::Axis) = font(
ax[:guidefontrotation],
ax[:guidefontcolor],
)
# ---------------------------------------------------------------
# converts unicode scientific notation unsupported by pgfplots and gr
# into a format that works
function convert_sci_unicode(label::AbstractString)
unicode_dict = Dict(
'⁰' => "0",
'¹' => "1",
'²' => "2",
'³' => "3",
'⁴' => "4",
'⁵' => "5",
'⁶' => "6",
'⁷' => "7",
'⁸' => "8",
'⁹' => "9",
'⁻' => "-",
"×10" => "×10^{",
)
for key in keys(unicode_dict)
label = replace(label, key, unicode_dict[key])
end
string(label, "}")
end