0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Juliaでインタラクティブプロット(Rectangle Selector編)

Posted at

本稿では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()

スクリーンショット 2018-05-10 22.36.24.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?