Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Oct 13, 2024
1 parent 393bfe9 commit 104e372
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/StatsPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ using NaNMath

export @df, dataviewer

isvertical(plotattributes) = let val = get(plotattributes, :orientation, missing)
val === missing || val in (:vertical, :v)
end

include("df.jl")
include("interact.jl")
include("corrplot.jl")
Expand Down
10 changes: 5 additions & 5 deletions src/boxplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ notch_width(q2, q4, N) = 1.58 * (q4 - q2) / sqrt(N)
)
# if only y is provided, then x will be UnitRange 1:size(y,2)
if typeof(x) <: AbstractRange
if step(x) == first(x) == 1
x = plotattributes[:series_plotindex]
x = if step(x) == first(x) == 1
plotattributes[:series_plotindex]
else
x = [getindex(x, plotattributes[:series_plotindex])]
[getindex(x, plotattributes[:series_plotindex])]
end
end
xsegs, ysegs = Segments(), Segments()
xsegs, ysegs = Plots.PlotsBase.Segments(), Plots.PlotsBase.Segments()
texts = String[]
glabels = sort(collect(unique(x)))
warning = false
Expand Down Expand Up @@ -162,7 +162,7 @@ notch_width(q2, q4, N) = 1.58 * (q4 - q2) / sqrt(N)
)
end

if !PlotsBase.isvertical(plotattributes)
if !isvertical(plotattributes)
# We should draw the plot horizontally!
xsegs, ysegs = ysegs, xsegs
outliers_x, outliers_y = outliers_y, outliers_x
Expand Down
4 changes: 2 additions & 2 deletions src/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
newx, newy =
violin_coords(y, trim = trim, wts = plotattributes[:weights], bandwidth = bandwidth)
if PlotsBase.isvertical(plotattributes)
if isvertical(plotattributes)
newx, newy = newy, newx
end
x := newx
Expand All @@ -37,7 +37,7 @@ PlotsBase.@deps density path
newx, newy =
violin_coords(y, trim = trim, wts = plotattributes[:weights], bandwidth = bandwidth)

if PlotsBase.isvertical(plotattributes)
if isvertical(plotattributes)
newx, newy = newy, newx
end

Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,24 @@ end
pl = violin(repeat([.1, .2, .3], outer=100), randn(300), side=:right)
@test show(devnull, pl) isa Nothing
end

@testset "density" begin
rng = StableRNG(1337)
pl = density(rand(100_000), label="density(rand())")
@test show(devnull, pl) isa Nothing
end

@testset "boxplot" begin
# credits to stackoverflow.com/a/71467031
boxed = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 7, 26, 80, 170, 322, 486, 688, 817, 888, 849, 783, 732, 624, 500, 349, 232, 130, 49], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 28, 83, 181, 318, 491, 670, 761, 849, 843, 862, 799, 646, 481, 361, 225, 98, 50], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 8, 28, 80, 179, 322, 493, 660, 753, 803, 832, 823, 783, 657, 541, 367, 223, 121, 62], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 23, 84, 171, 312, 463, 640, 778, 834, 820, 763, 752, 655, 518, 374, 244, 133, 52], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 21, 70, 169, 342, 527, 725, 808, 861, 857, 799, 688, 622, 523, 369, 232, 115, 41], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 9, 28, 76, 150, 301, 492, 660, 760, 823, 862, 790, 749, 646, 525, 352, 223, 116, 54], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 21, 64, 165, 290, 434, 585, 771, 852, 847, 785, 739, 630, 535, 354, 230, 114, 42], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 19, 76, 190, 337, 506, 680, 775, 851, 853, 816, 705, 588, 496, 388, 232, 127, 54]]

boxes = -0.002:0.0001:0.0012

xx = repeat(boxes, outer = length(boxed))
yy = collect(Iterators.flatten(boxed))

xtick = collect(-0.002:0.0005:0.0012)

pl = boxplot(xx * 20_000, yy, xticks = (xtick * 20_000, xtick))
@test show(devnull, pl) isa Nothing
end

0 comments on commit 104e372

Please sign in to comment.