Skip to content

Commit

Permalink
Merge pull request #7 from tpgillam/tg/twinx_convex_hull
Browse files Browse the repository at this point in the history
Implement subplot awareness for lims_convex_hull
  • Loading branch information
tpgillam authored Nov 7, 2022
2 parents bc7ce78 + a788434 commit 17745ef
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PlotIter"
uuid = "570a651b-f435-48ab-b76d-fb07b459bafb"
authors = ["Tom Gillam <[email protected]>"]
version = "0.1.3"
version = "0.2.0"

[deps]
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Expand Down
37 changes: 32 additions & 5 deletions src/PlotIter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function clims!(p::Plots.Subplot, lims::Tuple{Float64,Float64})
end
clims!(p::Plots.Subplot, cmin::Real, cmax::Real) = clims!(p, Float64.((cmin, cmax)))
function clims!(p::Plots.Plot, cmin::Real, cmax::Real)
# TODO Work out if applying clims to each subplot is the equivalent behaviour of xlims!
foreach(1:length(p)) do sp_idx
sp = p[sp_idx]
clims!(sp, cmin, cmax)
Expand All @@ -36,16 +35,21 @@ for dim in (:x, :y, :z, :c)
Set the $($dim_str)-axis limits for all `plots` to the smallest interval that
contains all the existing $($dim_str)-axis limits.
If the arguments are `Subplot`s, then there is no ambiguity over the limits of a
given argument. However, if they are `Plot`s, then we do the following:
* Let `n` be the minimum number of subplots across all arguments.
* For `i` in `1:n`, apply $($func!) to the all of the `i`th subplots.
This is useful to ensure that two plots are visually comparable.
"""
function $func!(
plots::Union{Tuple{Vararg{AbstractPlot}},AbstractVector{<:AbstractPlot}}
plots::Union{Tuple{Vararg{Plots.Subplot}},AbstractVector{<:Plots.Subplot}}
)
isempty(plots) && throw(ArgumentError("Need at least one plot."))

(x_min, x_max) = $lims(first(plots))
x_min, x_max = $lims(first(plots))
for p in plots[2:end]
(this_x_min, this_x_max) = $lims(p)
this_x_min, this_x_max = $lims(p)
x_min = min(x_min, this_x_min)
x_max = max(x_max, this_x_max)
end
Expand All @@ -56,7 +60,30 @@ for dim in (:x, :y, :z, :c)

return nothing
end
$func!(plots::AbstractPlot...) = $func!(plots)
$func!(plots::Plots.Subplot...) = $func!(plots)

function $func!(
plots::Union{Tuple{Vararg{AbstractPlot}},AbstractVector{<:AbstractPlot}}
)
isempty(plots) && throw(ArgumentError("Need at least one plot."))

# Determine the number of subplots for which we can apply convex hull behaviour.
n = minimum(
map(plots) do p
length(p.subplots)
end,
)

# Apply convex hull logic.
for i in 1:n
$func!(
map(plots) do p
p.subplots[i]
end,
)
end
end
$func!(plots::Plots.AbstractPlot...) = $func!(plots)
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ _allequal(things) = all(isequal(first(things)), things)
_test_lims_cover_range(lims1, 1:15)
end

@testset "ylims_convex_hull_twinx" begin
# Ensure that we correctly scale each y-axis separately when we have a secondary
# axis created with twinx.
range1 = 1:10
range2 = 3:15
range3 = 4:19
range4 = 10:25

p1 = plot(range1, range1)
plot!(twinx(p1), range2, range2)
@test length(p1.subplots) == 2
_test_lims_cover_range(ylims(p1.subplots[1]), range1)
_test_lims_cover_range(ylims(p1.subplots[2]), range2)

p2 = plot(range3, range3)
plot!(twinx(p2), range4, range4)
@test length(p2.subplots) == 2
_test_lims_cover_range(ylims(p2.subplots[1]), range3)
_test_lims_cover_range(ylims(p2.subplots[2]), range4)

ylims_convex_hull!(p1, p2)

@test ylims(p1.subplots[1]) == ylims(p2.subplots[1])
@test ylims(p1.subplots[2]) == ylims(p2.subplots[2])

_test_lims_cover_range(ylims(p1.subplots[1]), 1:19)
_test_lims_cover_range(ylims(p1.subplots[2]), 3:25)
end

@testset "clims_convex_hull!" begin
hm1 = heatmap(10 .* rand(5, 5))
hm2 = heatmap(3 .+ 12 .* rand(5, 5))
Expand Down

2 comments on commit 17745ef

@tpgillam
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/71833

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 17745ef34158f41ade095510de98f90cd2843c60
git push origin v0.2.0

Please sign in to comment.