本稿ではJuliaによるmatplotlibを使ったインタラクティブプロット(Slider編)について紹介します.
基本的には本家サイトにあるmatplotlib.widgetsのサンプルソースの移植版です.

slider_demo.jl
#Translated from python to julia (reffer to https://matplotlib.org/examples/widgets/slider_demo.html)
#Julia v0.6.2
#PyCall v3.6.4

using PyCall
using PyPlot

const Slider = matplotlib[:widgets][:Slider]
const Button = matplotlib[:widgets][:Button]
const RadioButtons = matplotlib[:widgets][:RadioButtons]

fig, ax = subplots()
subplots_adjust(left=0.25, bottom=0.25)
t = 0.0:0.001:1.0
a0=5
f0=3
s = a0*sin.(2*π*f0*t)
l, = plot(t,s,lw=2,color="red")
axis([0, 1, -10, 10])

axcolor = "lightgoldenrodyellow"
axfreq = axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)

sfreq = Slider(axfreq, "Freq", 0.1, 30.0, valinit=f0)
samp = Slider(axamp, "Amp", 0.1, 10.0, valinit=a0)

function update(val)
  amp = samp[:val]
  freq = sfreq[:val]
  l[:set_ydata](amp*sin.(2*π*freq*t))
  fig[:canvas][:draw_idle]()
end
sfreq[:on_changed](update)
samp[:on_changed](update)

resetax = axes([0.8,0.025,0.1,0.04])
button = Button(resetax,"Reset",color=axcolor,hovercolor="0.925")

function reset_(event)
  sfreq[:reset]()
  samp[:reset]()
end
button[:on_clicked](reset_)

rax = axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ("red","blue","green"), active=0)

function colorfunc(label)
  l[:set_color](label)
  fig[:canvas][:draw_idle]()
end
radio[:on_clicked](colorfunc)

show()

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

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.