PGFPlotsX: fix log scale power with custom ticks (#3961)

This commit is contained in:
t-bltg 2021-11-29 09:31:47 +01:00 committed by Zhanibek
parent 52fd2b6a75
commit 63743f1c17

View File

@ -1226,6 +1226,18 @@ function pgfx_sanitize_plot!(plt)
end end
## ##
end end
function wrap_power_labels(ticks)
# wrap the power part of label with }
tick_labels = similar(ticks)
for (i, label) in enumerate(ticks)
base, power = split(label, "^")
power = string("{", power, "}")
tick_labels[i] = string(base, "^", power)
end
tick_labels
end
# -------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------
function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter) function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter)
axis = sp[get_attr_symbol(letter, :axis)] axis = sp[get_attr_symbol(letter, :axis)]
@ -1284,7 +1296,8 @@ function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter)
# scale # scale
scale = axis[:scale] scale = axis[:scale]
if scale in (:log2, :ln, :log10) is_log_scale = scale in (:ln, :log2, :log10)
if is_log_scale
push!(opt, string(letter, :mode) => "log") push!(opt, string(letter, :mode) => "log")
scale == :ln || push!(opt, "log basis $letter" => "$(scale == :log2 ? 2 : 10)") scale == :ln || push!(opt, "log basis $letter" => "$(scale == :log2 ? 2 : 10)")
end end
@ -1297,11 +1310,7 @@ function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter)
end end
# grid on or off # grid on or off
if axis[:grid] && framestyle != :none push!(opt, "$(letter)majorgrids" => axis[:grid] && framestyle != :none ? "true" : "false")
push!(opt, "$(letter)majorgrids" => "true")
else
push!(opt, "$(letter)majorgrids" => "false")
end
# limits # limits
lims = lims =
@ -1317,14 +1326,8 @@ function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter)
ispolar(sp) && letter == :x ? [rad2deg.(ticks[1])[3:end]..., 360, 405] : ispolar(sp) && letter == :x ? [rad2deg.(ticks[1])[3:end]..., 360, 405] :
ticks[1] ticks[1]
push!(opt, string(letter, "tick") => string("{", join(tick_values, ","), "}")) push!(opt, string(letter, "tick") => string("{", join(tick_values, ","), "}"))
if axis[:showaxis] && axis[:scale] in (:ln, :log2, :log10) && axis[:ticks] == :auto if axis[:showaxis] && is_log_scale && axis[:ticks] == :auto
# wrap the power part of label with } tick_labels = wrap_power_labels(ticks[2])
tick_labels = similar(ticks[2])
for (i, label) in enumerate(ticks[2])
base, power = split(label, "^")
power = string("{", power, "}")
tick_labels[i] = string(base, "^", power)
end
if tick_labels isa Vector{String} if tick_labels isa Vector{String}
push!( push!(
opt, opt,
@ -1335,8 +1338,8 @@ function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter)
push!(opt, string(letter, "ticklabels") => join(tick_labels)) push!(opt, string(letter, "ticklabels") => join(tick_labels))
end end
elseif axis[:showaxis] elseif axis[:showaxis]
tick_labels = tick_labels = ispolar(sp) && letter == :x ? [ticks[2][3:end]..., "0", "45"] : ticks[2]
ispolar(sp) && letter == :x ? [ticks[2][3:end]..., "0", "45"] : ticks[2] is_log_scale && (tick_labels = wrap_power_labels(tick_labels))
if axis[:formatter] in (:scientific, :auto) && tick_labels isa Vector{String} if axis[:formatter] in (:scientific, :auto) && tick_labels isa Vector{String}
tick_labels = string.("\$", convert_sci_unicode.(tick_labels), "\$") tick_labels = string.("\$", convert_sci_unicode.(tick_labels), "\$")
tick_labels = replace.(tick_labels, Ref("×" => "\\times")) tick_labels = replace.(tick_labels, Ref("×" => "\\times"))