Skip to content

Commit

Permalink
Merge pull request #26 from jw3126/inset-feature
Browse files Browse the repository at this point in the history
Inset feature
  • Loading branch information
briochemc authored May 26, 2020
2 parents b6f4213 + 74b24a1 commit f3a3bf7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UnitfulRecipes"
uuid = "42071c24-d89e-48dd-8a24-8a12d9b8861f"
authors = ["Jan Weidner"]
version = "0.2.3"
version = "0.2.4"

[deps]
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Expand Down
11 changes: 11 additions & 0 deletions docs/lit/examples/1_Examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ plot(y)
y2 = 100randn(10)*u"g"
plot!(y2)


# UnitfulRecipes will not allow you to plot with different unit-dimensions, so
# ```julia
# plot!(rand(10)*u"m")
# ```
# won't work here.
#
# But you can add inset subplots with different axes that have different dimensions

plot!(rand(10)*u"m", inset=bbox(0.5, 0.5, 0.3, 0.3), subplot=2)

# ## Axis label

# If you specify an axis label, the unit will be appended to it.
Expand Down
32 changes: 17 additions & 15 deletions src/UnitfulRecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ function fixaxis!(attr, x, axisletter)
axis = Symbol(axisletter, :axis) # xaxis, yaxis, zaxis
# Get the unit
u = pop!(attr, axisunit, unit(eltype(x)))
if length(attr[:plot_object].subplots) > 0
label = attr[:plot_object][end][axis][:guide]
# If the subplot already exists, get unit from its axis label
sp = get(attr, :subplot, 1)
if sp length(attr[:plot_object])
label = attr[:plot_object][sp][axis][:guide]
if label isa UnitfulString
u = label.unit
end
Expand Down Expand Up @@ -69,22 +71,22 @@ end
@recipe function f(x::T1, y::T2, f::Function) where {T1<:AVec{<:Quantity}, T2<:AVec{<:Quantity}}
x, y, f.(x',y)
end
#@recipe f(xs::V, ys::UV, fun::Function) = recipe!(plotattributes, xs, ys, fun.(xs',ys))
#@recipe f(xs::UV, ys::V, fun::Function) = recipe!(plotattributes, xs, ys, fun.(xs',ys))
#


#==============
Attibute fixing
==============#
#===============
Attribute fixing
===============#

# Markers / lines
function fixmarkercolor!(attr)
u = ustripattribute!(attr, :marker_z)
fixlims!(attr, :clims, u)
u == Unitful.NoUnits || append_unit_if_needed!(attr, :colorbar_title, u)
end
fixmarkersize!(attr) = ustripattribute!(attr, :markersize)
fixlinecolor!(attr) = ustripattribute!(attr, :line_z)

# Lims
function fixlims!(attr, key, u)
if haskey(attr, key)
lims = attr[key]
Expand All @@ -94,7 +96,7 @@ function fixlims!(attr, key, u)
end
end

# strip unit from attribute
# strip unit from attribute[key]
function ustripattribute!(attr, key)
if haskey(attr, key)
v = attr[key]
Expand All @@ -107,9 +109,9 @@ function ustripattribute!(attr, key)
end


#===========
String stuff
===========#
#=======================================
Label string containing unit information
=======================================#

abstract type AbstractProtectedString <: AbstractString end
struct ProtectedString <: AbstractProtectedString
Expand Down Expand Up @@ -145,9 +147,9 @@ macro P_str(s)
end


#=============
label modifier
=============#
#=====================================
Append unit to labels when appropriate
=====================================#

function append_unit_if_needed!(attr, key, u::Unitful.Units)
label = get(attr, key, nothing)
Expand Down
49 changes: 38 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ using Test, Unitful, Plots
using Unitful: m, s, cm, DimensionError
using UnitfulRecipes

xguide(plt) = plt.subplots[end].attr[:xaxis].plotattributes[:guide]
yguide(plt) = plt.subplots[end].attr[:yaxis].plotattributes[:guide]
zguide(plt) = plt.subplots[end].attr[:zaxis].plotattributes[:guide]
xseries(plt) = plt.series_list[end].plotattributes[:x]
yseries(plt) = plt.series_list[end].plotattributes[:y]
zseries(plt) = plt.series_list[end].plotattributes[:z]
# Some helper functions to access the subplot labels and the series inside each test plot
xguide(plt, idx=length(plt.subplots)) = plt.subplots[idx].attr[:xaxis].plotattributes[:guide]
yguide(plt, idx=length(plt.subplots)) = plt.subplots[idx].attr[:yaxis].plotattributes[:guide]
zguide(plt, idx=length(plt.subplots)) = plt.subplots[idx].attr[:zaxis].plotattributes[:guide]
xseries(plt, idx=length(plt.series_list)) = plt.series_list[idx].plotattributes[:x]
yseries(plt, idx=length(plt.series_list)) = plt.series_list[idx].plotattributes[:y]
zseries(plt, idx=length(plt.series_list)) = plt.series_list[idx].plotattributes[:z]

@testset "plot(y)" begin
y = rand(3)m
Expand Down Expand Up @@ -67,6 +68,26 @@ end
end
end

@testset "With functions" begin
x, y = randn(3), randn(3)
@testset "plot(f, x) / plot(x, f)" begin
f(x) = x^2
@test plot( f, x*m) isa Plots.Plot
@test plot(x*m, f) isa Plots.Plot
g(x) = x*m # If the unit comes from the function only then it throws
@test_throws DimensionError plot(x, g) isa Plots.Plot
@test_throws DimensionError plot(g, x) isa Plots.Plot
end
@testset "plot(x, y, f)" begin
f(x,y) = x*y
@test plot(x*m, y*s, f) isa Plots.Plot
@test plot(x*m, y, f) isa Plots.Plot
@test plot( x, y*s, f) isa Plots.Plot
g(x,y) = x*y*m # If the unit comes from the function only then it throws
@test_throws DimensionError plot(x, y, g) isa Plots.Plot
end
end

@testset "Moar plots" begin
@testset "data as $dtype" for dtype in [:Vectors, :Matrices, Symbol("Vectors of vectors")]
if dtype == :Vectors
Expand All @@ -77,14 +98,12 @@ end
x, y, z = [rand(10), rand(20)], [rand(10), rand(20)], [rand(10), rand(20)]
end


@testset "One array" begin
@test plot(x*m) isa Plots.Plot
@test plot(x*m, ylabel="x") isa Plots.Plot
@test plot(x*m, ylims=(-1,1)) isa Plots.Plot
@test plot(x*m, ylims=(-1,1) .* m) isa Plots.Plot
@test plot(x*m, yunit=u"km") isa Plots.Plot
@test plot(x -> x^2, x*m) isa Plots.Plot
@test plot(x*m, yunit=u"km") isa Plots.Plot
end

@testset "Two arrays" begin
Expand Down Expand Up @@ -150,7 +169,6 @@ end
y = rand(10)*u"m"
@test plot(y, label=P"meters") isa Plots.Plot
end

end

@testset "Comparing apples and oranges" begin
Expand All @@ -162,4 +180,13 @@ end
@test yguide(plt) == "m"
@test yseries(plt) ustrip.(x2) / 100
@test_throws DimensionError plot!(plt, x3) # can't place seconds on top of meters!
end
end

@testset "Inset subplots" begin
x1 = rand(10) * u"m"
x2 = rand(10) * u"s"
plt = plot(x1)
plt = plot!(x2, inset=bbox(0.5, 0.5, 0.3, 0.3), subplot=2)
@test yguide(plt,1) == "m"
@test yguide(plt,2) == "s"
end

2 comments on commit f3a3bf7

@briochemc
Copy link
Collaborator 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/15409

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.4 -m "<description of version>" f3a3bf7b09cc90587a415abede80c9d350031385
git push origin v0.2.4

Please sign in to comment.