| 関数のグラフを描く, 追加して描く |
plot(sin) plot!(cos) plot!(x->sin(x)+cos(x)) |
| 複数の関数のグラフを描く |
plot([sin,cos,x->sin(x)+cos(x)])あるいは関数を定義して、その名前を使う。 f(x)=exp(-x*x/2)/sqrt(2*pi) plot(f) g(x)=sin(x)+cos(x) plot([sin,cos,g]) |
| 色々な体裁の調整 |
plot(sin,xlims=(-pi,pi),linecolor=:red)
plot!(cos,linecolor=:blue)
f(x)=sin(x)+cos(x)
plot!(f,linecolor=:green,title="sin,cos,sin+cos")
xlabel!("x")
|
| 自分で標本点を取って、関数値を計算して、プロットする |
N=100 x=range(0,2pi,length=N+1) plot(x,sin.(x)) plot(x,[sin.(x),cos.(x),sin.(x)+cos.(x)]) f(x)=sin(x)+cos(x) plot(x,[sin.(x),cos.(x),f.(x)]) |
最初にウィンドウが出るまでに待たされる。
| グラフィックスの保存 |
savefig("sin.pdf")
savefig("sin.png")
savefig("sin.ps")
|
PDFは美しい (この手の線画は得意だ)。 PNGは冗談?(調整できるのかな)、 PSはpoorな出来栄え(やる気なさそう)。
n=10; x = 1:n; y = rand(n); # These are the plotting data plot(x,y) plot(x,y,marker=:circle) |
| パラメーター曲線として楕円を描く |
N=100 t=range(0,2*pi,length=N+1) x=3*cos.(t) y=2*sin.(t) plot(x,y,aspect_ratio=1) |
| 対数目盛 |
plot([x->x,x->x^2,x->x^3],xlim=(0.1,3),xscale=:log10,yscale=:log10) |
次は、 Plots - powerful convenience for visualization in Julia にある例 (お題は Lorentz アトラクター) をやってみる。
| http://docs.juliaplots.org/latest/ |
using Plots
# define the Lorenz attractor
mutable struct Lorenz
dt; σ; ρ; β; x; y; z
end
function step!(l::Lorenz)
dx = l.σ*(l.y - l.x) ; l.x += l.dt * dx
dy = l.x*(l.ρ - l.z) - l.y ; l.y += l.dt * dy
dz = l.x*l.y - l.β*l.z ; l.z += l.dt * dz
end
attractor = Lorenz((dt = 0.02, σ = 10., ρ = 28., β = 8//3, x = 1., y = 1., z = 1.)...)
# initialize a 3D plot with 1 empty series
plt = plot3d(1, xlim=(-25,25), ylim=(-25,25), zlim=(0,50),
title = "Lorenz Attractor", marker = 2)
# build an animated gif by pushing new points to the plot, saving every 10th frame
@gif for i=1:1500
step!(attractor)
push!(plt, attractor.x, attractor.y, attractor.z)
end every 10
|
| GIFファイルを見てみよう、それから MP4 に変換してみる |
$ mv /var/folders/87/m5d8lr3n0z335t7yj64zg1qh0000gn/T/jl_gzDeDZ.gif lorentz.gif $ open lorentz.gifこれで紙芝居は出来る。動画にするには、いつもの通りで $ ffmpeg -i lorentz.gif -movflags faststart -pix_fmt yuv420p -vf \ "scale=trunc(iw/2)*2:trunc(ih/2)*2" lorentz.mp4 |
| title="文字列" | タイトル |
| xlabel="文字列" | 横軸のラベル |
| ylabel="文字列" | 縦軸のラベル |
| label="文字列" | 凡例のラベル |
| xlim=(x1,x2) | 横軸の範囲 |
| ylim=(y1,y2) | 縦軸の範囲 |
| aspect_ratio=a | アスペクト比 (縦高さ/横幅) |
| size=(W,H) | プロットのサイズ |
| linecolor=c | 線の色 (:red とか) |
| linewidth=w | 線の幅 |
| linestyle=線種 | 線の種類 (:dot とか) |