From 4f171e3eb540e8fca41246708c3f88bb06550645 Mon Sep 17 00:00:00 2001 From: Andrew Palugniok Date: Thu, 2 Nov 2017 16:32:57 +0000 Subject: [PATCH] Add handling of polar axes. --- src/axes.jl | 20 +++++++++++++++++--- src/backends/pgfplots.jl | 7 +++++-- src/backends/plotly.jl | 19 +++++++++++++++++++ src/backends/pyplot.jl | 5 +++++ 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/axes.jl b/src/axes.jl index 4288cde3..7ca04d2b 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -239,8 +239,13 @@ function get_ticks(axis::Axis) # discrete ticks... axis[:continuous_values], dvals elseif ticks == :auto - # compute optimal ticks and labels - optimal_ticks_and_labels(axis) + if ispolar(axis.sps[1]) && axis[:letter] == :x + #force theta axis to be full circle + (collect(0:pi/4:7pi/4), string.(0:45:315)) + else + # compute optimal ticks and labels + optimal_ticks_and_labels(axis) + end elseif typeof(ticks) <: Union{AVec, Int} # override ticks, but get the labels optimal_ticks_and_labels(axis, ticks) @@ -427,7 +432,16 @@ function axis_limits(axis::Axis, should_widen::Bool = default_should_widen(axis) if !isfinite(amin) && !isfinite(amax) amin, amax = 0.0, 1.0 end - if should_widen + if ispolar(axis.sps[1]) + if axis[:letter] == :x + amin, amax = 0, 2pi + elseif lims == :auto + #widen max radius so ticks dont overlap with theta axis + amin, 1.1*amax + else + amin, amax + end + elseif should_widen widen(amin, amax) else amin, amax diff --git a/src/backends/pgfplots.jl b/src/backends/pgfplots.jl index ef009fe1..5f73a674 100644 --- a/src/backends/pgfplots.jl +++ b/src/backends/pgfplots.jl @@ -220,6 +220,8 @@ function pgf_series(sp::Subplot, series::Series) # If a marker_z is used pass it as third coordinate to a 2D plot. # See "Scatter Plots" in PGFPlots documentation d[:x], d[:y], d[:marker_z] + elseif ispolar(sp) + rad2deg.(d[:x]), d[:y] else d[:x], d[:y] end @@ -297,14 +299,15 @@ function pgf_axis(sp::Subplot, letter) # limits # TODO: support zlims if letter != :z - lims = axis_limits(axis) + lims = ispolar(sp) && letter == :x ? rad2deg.(axis_limits(axis)) : axis_limits(axis) kw[Symbol(letter,:min)] = lims[1] kw[Symbol(letter,:max)] = lims[2] end if !(axis[:ticks] in (nothing, false, :none)) && framestyle != :none ticks = get_ticks(axis) - push!(style, string(letter, "tick = {", join(ticks[1],","), "}")) + tick_values = ispolar(sp) && letter == :x ? rad2deg.(ticks[1]) : ticks[1] + push!(style, string(letter, "tick = {", join(tick_values,","), "}")) if axis[:showaxis] && axis[:scale] in (:ln, :log2, :log10) && axis[:ticks] == :auto # wrap the power part of label with } tick_labels = String[begin diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index b15e82c0..e06f4151 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -291,6 +291,22 @@ function plotly_axis(axis::Axis, sp::Subplot) ax end +function plotly_polaraxis(axis::Axis) + ax = KW( + :visible => axis[:grid], + :showline => axis[:grid], + ) + + if axis[:letter] == :x + ax[:range] = rad2deg.(axis_limits(axis)) + else + ax[:range] = axis_limits(axis) + ax[:orientation] = 0 + end + + ax +end + function plotly_layout(plt::Plot) d_out = KW() @@ -345,6 +361,9 @@ function plotly_layout(plt::Plot) ), ), ) + elseif ispolar(sp) + d_out[Symbol("angularaxis$spidx")] = plotly_polaraxis(sp[:xaxis]) + d_out[Symbol("radialaxis$spidx")] = plotly_polaraxis(sp[:yaxis]) else d_out[Symbol("xaxis$spidx")] = plotly_axis(sp[:xaxis], sp) d_out[Symbol("yaxis$spidx")] = plotly_axis(sp[:yaxis], sp) diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index 4a8c3c95..9022a692 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -1054,6 +1054,9 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend}) end py_set_scale(ax, axis) py_set_lims(ax, axis) + if ispolar(sp) && letter == :y + ax[:set_rlabel_position](0) + end ticks = sp[:framestyle] == :none ? nothing : get_ticks(axis) # don't show the 0 tick label for the origin framestyle if sp[:framestyle] == :origin && length(ticks) > 1 @@ -1080,6 +1083,8 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend}) linewidth = axis[:gridlinewidth], alpha = axis[:gridalpha]) ax[:set_axisbelow](true) + else + pyaxis[:grid](false) end py_set_axis_colors(sp, ax, axis) end