From 32d59fff615df6fd098ca434c9faf1e2fb16f444 Mon Sep 17 00:00:00 2001 From: Andrew Palugniok Date: Thu, 16 Nov 2017 22:27:13 +0000 Subject: [PATCH 1/2] Turn off scaled ticks for PGFPlots. --- src/backends/pgfplots.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/backends/pgfplots.jl b/src/backends/pgfplots.jl index d83917d0..13f9daad 100644 --- a/src/backends/pgfplots.jl +++ b/src/backends/pgfplots.jl @@ -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]) From 62ca787e6e3d9076b062064a534095706b875889 Mon Sep 17 00:00:00 2001 From: Andrew Palugniok Date: Sat, 23 Dec 2017 00:35:44 +0000 Subject: [PATCH 2/2] Fix scientific formatter for GR and PGFPlots. --- src/backends/gr.jl | 25 +++++++++++++++++++++++-- src/backends/pgfplots.jl | 2 ++ src/utils.jl | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 881c5e8d..5e2d6ed6 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -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 diff --git a/src/backends/pgfplots.jl b/src/backends/pgfplots.jl index 13f9daad..db4e8b26 100644 --- a/src/backends/pgfplots.jl +++ b/src/backends/pgfplots.jl @@ -329,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 = {}")) @@ -500,6 +501,7 @@ end function _display(plt::Plot{PGFPlotsBackend}) # prepare the object + PGFPlots.pushPGFPlotsPreamble("\\usepackage{fontspec}") pgfplt = PGFPlots.plot(plt.o) # save an svg diff --git a/src/utils.jl b/src/utils.jl index 5d1222bd..3fc55a88 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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