本稿ではJuliaによるmatplotlibを使ったインタラクティブプロット(Rectangle Selector編)について紹介します.
基本的には本家サイトにあるmatplotlib.widgetsのサンプルソースの移植版です.
rectangle_selector.jl
#Translated from python to julia (reffer to https://matplotlib.org/examples/widgets/rectangle_selector.html)
#Julia v0.6.2
#PyCall v3.6.4
using PyCall
using PyPlot
const RectangleSelector = matplotlib[:widgets][:RectangleSelector]
function line_select_callback(eclick, erelease)
#eclick and erelease are the press and release events
x1 = eclick[:xdata]
y1 = eclick[:ydata]
x2 = erelease[:xdata]
y2 = erelease[:ydata]
@printf "(%3.2f, %3.2f) --> (%3.2f, %3.2f)" x1 y1 x2 y2
@printf " The button you used were: %s %s" eclick[:button] erelease[:button]
end
function toggle_selector(event)
println("Key pressed.")
if event[:key] in ["Q", "q"] && toggle_selector[:RS][:active]
println("RectangleSelector deactivated.")
toggle_selector[:RS][:set_active](false)
end
if event[:key] in ["A", "a"] && !toggle_selector[:RS][:active]
println("RectangleSelector activated.")
toggle_selector[:RS][:set_active](true)
end
end
fig, current_ax = plt[:subplots]() # make a new plotting range
N = 10 # If N is large one can see
x = 0:0.01:N
plt[:plot](x, sin.(.2*π*x), lw=3.5, c="b", alpha=.7) # plot something
plt[:plot](x, cos.(.2*π*x), lw=3.5, c="r", alpha=.5)
plt[:plot](x, -sin.(.2*π*x), lw=3.5, c="g", alpha=.3)
@printf "\n click --> release"
# drawtype is 'box' or 'line' or 'none'
RS = RectangleSelector(current_ax,
line_select_callback,
drawtype="box",
useblit=true,
button=[1, 3],
minspanx=5,
minspany=5,
spancoords="pixels",
interactive=true)
connect("key_press_event", toggle_selector)
show()