From c1c97a5fc1af8e542739725a60220a7a724d44b5 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Tue, 19 Jul 2016 10:32:23 -0400 Subject: [PATCH] labelfunc for pyplot --- src/axes.jl | 12 +++++++----- src/backends/plotly.jl | 2 +- src/backends/pyplot.jl | 13 +++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/axes.jl b/src/axes.jl index dba82b6e..08e1b93f 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -133,14 +133,12 @@ const _label_func = Dict{Symbol,Function}( :log10 => x -> "10^$x", :log2 => x -> "2^$x", :ln => x -> "e^$x", - # :log2 => exp2, - # :ln => exp, ) scalefunc(scale::Symbol) = x -> get(_scale_funcs, scale, identity)(Float64(x)) invscalefunc(scale::Symbol) = x -> get(_inv_scale_funcs, scale, identity)(Float64(x)) -labelfunc(scale::Symbol) = get(_label_func, scale, string) +labelfunc(scale::Symbol, backend::AbstractBackend) = get(_label_func, scale, string) function optimal_ticks_and_labels(axis::Axis, ticks = nothing) lims = axis_limits(axis) @@ -155,7 +153,7 @@ function optimal_ticks_and_labels(axis::Axis, ticks = nothing) optimize_ticks(scaled_lims..., k_min = 5, # minimum number of ticks k_max = 8, # maximum number of ticks - span_buffer = 0.0 # padding buffer in case nice ticks are closeby + # span_buffer = 0.0 # padding buffer in case nice ticks are closeby )[1] else ticks @@ -166,7 +164,11 @@ function optimal_ticks_and_labels(axis::Axis, ticks = nothing) # rescale and return values and labels # @show cv - ticklabels = map(labelfunc(scale), Showoff.showoff(cv, :plain)) + ticklabels = if any(isfinite, cv) + map(labelfunc(scale, backend()), Showoff.showoff(cv, :plain)) + else + UTF8String[] + end tickvals = map(invscalefunc(scale), cv) # @show tickvals ticklabels diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 07f5825f..3aee0a1e 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -191,7 +191,7 @@ function plotly_axis(axis::Axis, sp::Subplot) # ticks ticks = get_ticks(axis) - if ticks != :auto + if ticks != :auto && ax[:type] != "-" ttype = ticksType(ticks) if ttype == :ticks ax[:tickmode] = "array" diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index bd086a39..24efb1ba 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -54,6 +54,7 @@ function _initialize_backend(::PyPlotBackend) # solution: hack from @stevengj: https://github.com/stevengj/PyPlot.jl/pull/223#issuecomment-229747768 otherdisplays = splice!(Base.Multimedia.displays, 2:length(Base.Multimedia.displays)) import PyPlot + import LaTeXStrings: latexstring append!(Base.Multimedia.displays, otherdisplays) export PyPlot @@ -221,6 +222,18 @@ function add_pyfixedformatter(cbar, vals::AVec) end +function labelfunc(scale::Symbol, backend::PyPlotBackend) + if scale == :log10 + x -> latexstring("10^{$x}") + elseif scale == :log2 + x -> latexstring("2^{$x}") + elseif scale == :ln + x -> latexstring("e^{$x}") + else + string + end +end + # --------------------------------------------------------------------------- function fix_xy_lengths!(plt::Plot{PyPlotBackend}, series::Series)