Skip to content

Commit fa55365

Browse files
committed
errorbar(), tests
1 parent 4874eff commit fa55365

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

ext/AxisKeysExt.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module AxisKeysExt
2-
using AxisKeys: KeyedArray, axiskeys, dimnames
2+
using AxisKeys: KeyedArray, axiskeys, dimnames, keyless_unname
33
using PyPlotUtils.IntervalSets
44
using PyPlotUtils
5-
import PyPlotUtils: extent_ax, extent_arr, plot_ax, fill_between_ax, pcolormesh_ax
5+
import PyPlotUtils: extent_ax, extent_arr, plot_ax, fill_between_ax, errorbar_ax, pcolormesh_ax
66

77
extent_arr(A::KeyedArray) = (extent_ax(axiskeys(A, 1))..., extent_ax(axiskeys(A, 2))...)
88

@@ -14,6 +14,10 @@ function fill_between_ax(A::KeyedArray; kwargs...)
1414
plt.fill_between(only(axiskeys(A)), leftendpoint.(A), rightendpoint.(A); kwargs...)
1515
end
1616

17+
function errorbar_ax(A::KeyedArray; kwargs...)
18+
errorbar(only(axiskeys(A)), keyless_unname(A); kwargs...)
19+
end
20+
1721
function pcolormesh_ax(A::KeyedArray; kwargs...)
1822
res = plt.pcolormesh(
1923
axiskeys(A, 1),

src/PyPlotUtils.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export
2323
keep_plt_lims, set_xylims, lim_intersect, lim_union,
2424
set_xylabels, xylabels, label_log_scales, draw_text_along, legend_inline_right,
2525
imshow_ax, SymLog, ColorBar,
26-
pcolormesh_ax, plot_ax, fill_between_ax,
26+
pcolormesh_ax, plot_ax, fill_between_ax, errorbar, errorbar_ax,
2727
mpl_color, adjust_lightness,
2828
axplotfunc, add_zoom_patch,
2929
ScalebarArtist
@@ -202,8 +202,22 @@ end
202202
# XXX: generalize somehow? like axiskeys(pcolormesh)(A) or ...?
203203
function plot_ax end
204204
function fill_between_ax end
205+
function errorbar_ax end
205206
function pcolormesh_ax end
206207

208+
errorbar(x, y; kwargs...) = errorbar([x], [y]; kwargs...)
209+
function errorbar(x::AbstractVector, y::AbstractVector; midfunc=mean, kwargs...)
210+
xmids = midfunc.(x)
211+
xerr = map(x) do x
212+
abs.(extrema(x) .- midfunc(x))
213+
end |> stack
214+
ymids = midfunc.(y)
215+
yerr = map(y) do x
216+
abs.(extrema(x) .- midfunc(x))
217+
end |> stack
218+
plt.errorbar(xmids, ymids; xerr, yerr, kwargs...)
219+
end
220+
207221
""" axplotfunc(f; ax=plt.gca(), n=10, [plot() kwargs...])
208222
Plot `y = f(x)` within the current axis limits. Uses `n` points that uniformly split the whole `x` interval. """
209223
function axplotfunc(f; ax=plt.gca(), n=10, kwargs...)

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
44
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
55
CompatHelperLocal = "5224ae11-6099-4aaa-941d-3aab004bd678"
6+
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
67
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
78
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
89
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
@@ -13,6 +14,7 @@ Aqua = "0.6"
1314
AxisKeys = "0.2"
1415
Colors = "0.12"
1516
CompatHelperLocal = "0.1"
17+
IntervalSets = "0.7"
1618
OffsetArrays = "1.10"
1719
PyPlot = "2.9"
1820
Unitful = "1.9"

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using PyPlotUtils
2+
using IntervalSets
23
using AxisKeys
34
using OffsetArrays
45
using Unitful
@@ -42,6 +43,15 @@ imshow_ax(KeyedArray([1 2; 3 4], a=(-5:-4)u"m", b=(0:1)u"m"))
4243
imshow_ax(KeyedArray([1 2; 3 4], a=(-5:-4)u"m", b=(0:1)u"m"), ColorBar())
4344
imshow_ax(KeyedArray([1 2; 3 4], a=(-5:-4)u"m", b=(0:1)u"m"), ColorBar(); norm=SymLog())
4445

46+
plot_ax(KeyedArray([1, 2, 3], a=5:7))
47+
fill_between_ax(KeyedArray([1..2, 2..3, 3..4], a=5:7))
48+
errorbar_ax(KeyedArray([1..2, 2..3, 3..4], a=5:7))
49+
pcolormesh_ax(KeyedArray([1 2; 3 4], a=-5:-4, b=0:1))
50+
errorbar(1, 1..2)
51+
errorbar(1..2, 1)
52+
errorbar(1..2, 1..2)
53+
errorbar([1..2], [1..2])
54+
4555
plt.gca().add_artist(ScalebarArtist([(1, "px")]))
4656

4757
axplotfunc(sin)

0 commit comments

Comments
 (0)