From 2fe2ff474c3871400300371847709fa9e2859a59 Mon Sep 17 00:00:00 2001 From: Andrew Palugniok Date: Sat, 4 Nov 2017 21:27:02 +0000 Subject: [PATCH] Implement grid attributes for polar plots. --- src/backends/gr.jl | 85 +++++++++++++++++++++++++++------------- src/backends/pgfplots.jl | 2 + src/backends/plotly.jl | 2 +- src/backends/pyplot.jl | 10 ++++- 4 files changed, 69 insertions(+), 30 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index ef539066..cfb6e85d 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -205,35 +205,66 @@ function gr_text(x, y, s) end end -function gr_polaraxes(rmin, rmax) +function gr_polaraxes(rmin::Real, rmax::Real, sp::Subplot) GR.savestate() - GR.setlinetype(GR.LINETYPE_SOLID) - GR.setlinecolorind(88) + xaxis = sp[:xaxis] + yaxis = sp[:yaxis] + + α = 0:45:315 + a = α .+ 90 + sinf = sind.(a) + cosf = cosd.(a) tick = 0.5 * GR.tick(rmin, rmax) n = round(Int, (rmax - rmin) / tick + 0.5) - for i in 0:n - r = float(i) / n - if i % 2 == 0 - GR.setlinecolorind(88) - if i > 0 - GR.drawarc(-r, r, -r, r, 0, 359) - end - GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) - x, y = GR.wctondc(0.05, r) - GR.text(x, y, string(signif(rmin + i * tick, 12))) - else - GR.setlinecolorind(90) - GR.drawarc(-r, r, -r, r, 0, 359) + + #draw angular grid + if xaxis[:grid] + gr_set_line(xaxis[:gridlinewidth], xaxis[:gridstyle], xaxis[:foreground_color_grid]) + GR.settransparency(xaxis[:gridalpha]) + for i in 1:length(α) + GR.polyline([sinf[i], 0], [cosf[i], 0]) end end - for α in 0:45:315 - a = α + 90 - sinf = sin(a * pi / 180) - cosf = cos(a * pi / 180) - GR.polyline([sinf, 0], [cosf, 0]) - GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_HALF) - x, y = GR.wctondc(1.1 * sinf, 1.1 * cosf) - GR.textext(x, y, string(α, "^o")) + + #draw radial grid + if yaxis[:grid] + gr_set_line(yaxis[:gridlinewidth], yaxis[:gridstyle], yaxis[:foreground_color_grid]) + GR.settransparency(yaxis[:gridalpha]) + for i in 0:n + r = float(i) / n + if i % 2 == 0 + if i > 0 + GR.drawarc(-r, r, -r, r, 0, 359) + end + else + GR.drawarc(-r, r, -r, r, 0, 359) + end + end + end + + #prepare to draw ticks + GR.settransparency(1) + GR.setlinecolorind(90) + GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_HALF) + + #draw angular ticks + if xaxis[:showaxis] + GR.drawarc(-1, 1, -1, 1, 0, 359) + for i in 1:length(α) + x, y = GR.wctondc(1.1 * sinf[i], 1.1 * cosf[i]) + GR.textext(x, y, string((360-α[i])%360, "^o")) + end + end + + #draw radial ticks + if yaxis[:showaxis] + for i in 0:n + r = float(i) / n + if i % 2 == 0 + x, y = GR.wctondc(0.05, r) + GR.text(x, y, string(signif(rmin + i * tick, 12))) + end + end end GR.restorestate() end @@ -760,9 +791,9 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas) elseif ispolar(sp) r = gr_set_viewport_polar() - rmin, rmax = GR.adjustrange(ignorenan_minimum(r), ignorenan_maximum(r)) - # rmin, rmax = axis_limits(sp[:yaxis]) - gr_polaraxes(rmin, rmax) + #rmin, rmax = GR.adjustrange(ignorenan_minimum(r), ignorenan_maximum(r)) + rmin, rmax = axis_limits(sp[:yaxis]) + gr_polaraxes(rmin, rmax, sp) elseif draw_axes if xmax > xmin && ymax > ymin diff --git a/src/backends/pgfplots.jl b/src/backends/pgfplots.jl index e0f35f4d..da527f9a 100644 --- a/src/backends/pgfplots.jl +++ b/src/backends/pgfplots.jl @@ -294,6 +294,8 @@ function pgf_axis(sp::Subplot, letter) # grid on or off if axis[:grid] && framestyle != :none push!(style, "$(letter)majorgrids = true") + else + push!(style, "$(letter)majorgrids = false") end # limits diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 940e36f6..9ada51f7 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -293,7 +293,7 @@ end function plotly_polaraxis(axis::Axis) ax = KW( - :visible => axis[:grid], + :visible => axis[:showaxis], :showline => axis[:grid], ) diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index 86a8b5a0..d325b21b 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -1093,7 +1093,11 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend}) if !sp[:xaxis][:showaxis] kw = KW() for dir in (:top, :bottom) - ax[:spines][string(dir)][:set_visible](false) + if ispolar(sp) + ax[:spines]["polar"][:set_visible](false) + else + ax[:spines][string(dir)][:set_visible](false) + end kw[dir] = kw[Symbol(:label,dir)] = "off" end ax[:xaxis][:set_tick_params](; which="both", kw...) @@ -1101,7 +1105,9 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend}) if !sp[:yaxis][:showaxis] kw = KW() for dir in (:left, :right) - ax[:spines][string(dir)][:set_visible](false) + if !ispolar(sp) + ax[:spines][string(dir)][:set_visible](false) + end kw[dir] = kw[Symbol(:label,dir)] = "off" end ax[:yaxis][:set_tick_params](; which="both", kw...)