Add handling of polar axes.

This commit is contained in:
Andrew Palugniok 2017-11-02 16:32:57 +00:00
parent 671f5c3a68
commit 4f171e3eb5
4 changed files with 46 additions and 5 deletions

View File

@ -239,8 +239,13 @@ function get_ticks(axis::Axis)
# discrete ticks... # discrete ticks...
axis[:continuous_values], dvals axis[:continuous_values], dvals
elseif ticks == :auto elseif ticks == :auto
# compute optimal ticks and labels if ispolar(axis.sps[1]) && axis[:letter] == :x
optimal_ticks_and_labels(axis) #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} elseif typeof(ticks) <: Union{AVec, Int}
# override ticks, but get the labels # override ticks, but get the labels
optimal_ticks_and_labels(axis, ticks) 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) if !isfinite(amin) && !isfinite(amax)
amin, amax = 0.0, 1.0 amin, amax = 0.0, 1.0
end 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) widen(amin, amax)
else else
amin, amax amin, amax

View File

@ -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. # If a marker_z is used pass it as third coordinate to a 2D plot.
# See "Scatter Plots" in PGFPlots documentation # See "Scatter Plots" in PGFPlots documentation
d[:x], d[:y], d[:marker_z] d[:x], d[:y], d[:marker_z]
elseif ispolar(sp)
rad2deg.(d[:x]), d[:y]
else else
d[:x], d[:y] d[:x], d[:y]
end end
@ -297,14 +299,15 @@ function pgf_axis(sp::Subplot, letter)
# limits # limits
# TODO: support zlims # TODO: support zlims
if letter != :z 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,:min)] = lims[1]
kw[Symbol(letter,:max)] = lims[2] kw[Symbol(letter,:max)] = lims[2]
end end
if !(axis[:ticks] in (nothing, false, :none)) && framestyle != :none if !(axis[:ticks] in (nothing, false, :none)) && framestyle != :none
ticks = get_ticks(axis) 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 if axis[:showaxis] && axis[:scale] in (:ln, :log2, :log10) && axis[:ticks] == :auto
# wrap the power part of label with } # wrap the power part of label with }
tick_labels = String[begin tick_labels = String[begin

View File

@ -291,6 +291,22 @@ function plotly_axis(axis::Axis, sp::Subplot)
ax ax
end 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) function plotly_layout(plt::Plot)
d_out = KW() 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 else
d_out[Symbol("xaxis$spidx")] = plotly_axis(sp[:xaxis], sp) d_out[Symbol("xaxis$spidx")] = plotly_axis(sp[:xaxis], sp)
d_out[Symbol("yaxis$spidx")] = plotly_axis(sp[:yaxis], sp) d_out[Symbol("yaxis$spidx")] = plotly_axis(sp[:yaxis], sp)

View File

@ -1054,6 +1054,9 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend})
end end
py_set_scale(ax, axis) py_set_scale(ax, axis)
py_set_lims(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) ticks = sp[:framestyle] == :none ? nothing : get_ticks(axis)
# don't show the 0 tick label for the origin framestyle # don't show the 0 tick label for the origin framestyle
if sp[:framestyle] == :origin && length(ticks) > 1 if sp[:framestyle] == :origin && length(ticks) > 1
@ -1080,6 +1083,8 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend})
linewidth = axis[:gridlinewidth], linewidth = axis[:gridlinewidth],
alpha = axis[:gridalpha]) alpha = axis[:gridalpha])
ax[:set_axisbelow](true) ax[:set_axisbelow](true)
else
pyaxis[:grid](false)
end end
py_set_axis_colors(sp, ax, axis) py_set_axis_colors(sp, ax, axis)
end end