I want to automatically plot arrays of Measurement
’s as scatterplots with errorbars using PyPlot.jl
. For the interactive PyPlot interface adding the following method to scatter()
works nicely:
using PyPlot
import PyPlot: scatter
import Measurements: value, uncertainty
function scatter(x::AbstractArray{<:Measurement}, y::AbstractArray{<:Measurement}, args...; kwargs...)
errorbar(value.(x), value.(y), uncertainty.(y), uncertainty.(x), args...; kwargs...)
end
scatter(x, y::AbstractArray{<:Measurement}, args...; kwargs...) = scatter(x .± 0, y, args...; kwargs...)
scatter(x::AbstractArray{<:Measurement}, y, args...; kwargs...) = scatter(x, y .± 0, args...; kwargs...)
# Test the new scatter
x = 1:0.1:3
y = sin.(x) .± 0.2cos.(x)
scatter(x, y, marker="s", color="C2")
However, I don’t know how to preceed to get a similar effect with the object-oriented interface of matplotlib. I.e. I want to be able to do
fig, ax = PyPlot.subplots(figsize=(6,4))
x = 1:0.1:3
y = sin.(x) .± 0.2cos.(x)
ax.scatter(x, y, marker="s", color="C2")
and get the same plot as before.
I guess I would have to somehow overload getproperty(::PyObject, ::Symbol)
to get a different method returned for getproperty(ax, :plot)
. But since ax
is of type PyObject
, I can’t really dispatch on that without affecting all other PyObjects
as well. Also, is it possible to add methods to a PyObject <bound method Axes.plot of <AxesSubplot:>>
, or is that all unchangeable from julia?