Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add polarheatmap #318

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/UnicodePlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export scatterplot!,
stairs

# methods without mutating variants
export horizontal_histogram, vertical_histogram, histogram, heatmap, spy, imageplot
export horizontal_histogram, vertical_histogram, histogram, polarheatmap, heatmap, spy, imageplot

include("common.jl")
include("lut.jl")
Expand Down Expand Up @@ -113,6 +113,7 @@ include("interface/heatmap.jl")
include("interface/spy.jl")
include("interface/boxplot.jl")
include("interface/polarplot.jl")
include("interface/polarheatmap.jl")
include("interface/imageplot.jl")

function __init__()
Expand Down
25 changes: 25 additions & 0 deletions src/interface/polarheatmap.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
# Author(s)

- T Bltg (github.com/t-bltg)

# Examples

"""
function polarheatmap(
θ::AbstractVector,
r::Union{Function,AbstractVector},
A::Union{Function,AbstractVecOrMat};
rlim = (0, 0),
kw...
)
r, lims = polar_lims(θ, r, rlim)
A = A isa Function ? A.(θ, r) : A

# reuse polarplot to draw the grid
plot = polarplot(zero(lims), lims)

# use vec(A) ..., should we add yet another canvas ??

plot
end
41 changes: 23 additions & 18 deletions src/interface/polarplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ function polarplot(
kw...,
)
pkw, okw = split_plot_kw(kw)

r = r isa Function ? r.(θ) : r
max_r = last(is_auto(rlim) ? extrema(r) : rlim)
lims = x = y = [-max_r, +max_r]
r, lims = polar_lims(θ, r, rlim)
plot = Plot(
x,
y;
lims,
lims;
xlim = lims,
ylim = lims,
grid = false,
Expand All @@ -92,35 +89,43 @@ end
scatter = false,
kw...,
)
mr, Mr = is_auto(rlim) ? extrema(r) : rlim
mr, Mr = rlim = collect(rlim)

# grid
theta = range(0, 2π, length = 360)
grid_color = BORDER_COLOR[]
lineplot!(plot, Mr * cos.(theta), Mr * sin.(theta), color = grid_color)
color = BORDER_COLOR[]
lineplot!(plot, Mr * cos.(theta), Mr * sin.(theta); color)

for theta ∈ 0:(π / 4):(2π)
lineplot!(plot, [mr, Mr] .* cos(theta), [mr, Mr] .* sin(theta); color = grid_color)
lineplot!(plot, rlim .* cos(theta), rlim .* sin(theta); color)
end

# user data
(scatter ? scatterplot! : lineplot!)(plot, r .* cos.(θ), r .* sin.(θ); kw...)

# labels
row = ceil(Int, nrows(plot.graphics) / 2)
label!(plot, :r, row, degrees ? "0°" : "0", color = grid_color)
label!(plot, :t, degrees ? "90°" : "π / 2", color = grid_color)
label!(plot, :l, row, degrees ? "180°" : "π", color = grid_color)
label!(plot, :b, degrees ? "270°" : "3π / 4", color = grid_color)
label!(plot, :r, row, degrees ? "0°" : "0"; color)
label!(plot, :t, degrees ? "90°" : "π / 2"; color)
label!(plot, :l, row, degrees ? "180°" : "π"; color)
label!(plot, :b, degrees ? "270°" : "3π / 4"; color)

for r ∈ range(mr, Mr, length = num_rad_lab)
annotate!(
plot,
r * cos(ang_rad_lab),
r * sin(ang_rad_lab),
isinteger(r) ? string(round(Int, r)) : @sprintf("%.1f", r);
color = grid_color,
color,
)
end

# user data
(scatter ? scatterplot! : lineplot!)(plot, r .* cos.(θ), r .* sin.(θ); kw...)

plot
end

function polar_lims(θ::AbstractVector, r::Union{Function,AbstractVector}, rlim)
r = r isa Function ? r.(θ) : r
r_max = last(is_auto(rlim) ? extrema(r) : rlim)
lims = x = y = [-r_max, +r_max]
r, lims
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ withenv("FORCE_COLOR" => "X") do # JuliaPlots/UnicodePlots.jl/issues/134
@timeit_include "tst_boxplot.jl"
@timeit_include "tst_contourplot.jl"
@timeit_include "tst_polarplot.jl"
@timeit_include "tst_polarheatmap.jl"
@timeit_include "tst_heatmap.jl"
@timeit_include "tst_volume.jl"
@timeit_include "tst_surfaceplot.jl"
Expand Down
8 changes: 8 additions & 0 deletions test/tst_polarheatmap.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@testset "simple" begin
θ = range(0, 2π; length = 100)
r = range(0, 120; length = 50)
A = sin.(r ./ 10) .* (cos.(θ))'
p = polarheatmap(θ, r, A)

test_ref("polarheatmap/simple.txt", @show_col(p))
end