Julia の Plots を使用して極座標グラフ (Polar Plot) を描くには、既存の関数 plot() に属性 proj = :polar (または projection = :polar) を設定する方法が簡単です。plot() の第 1 引数に角度、第 2 引数に半径を渡します。角度はラジアンで指定します。
リスト : バラ曲線 using Plots theta = range(0, 2 * pi, length=100) # 角度 r1 = sin.(3 * theta) # 半径 r2 = sin.(2 * theta) r3 = sin.(5 * theta) plot(theta, [r1, r2, r3], proj = :polar, title = "rosecurve")
バラ曲線
リスト : アルキメデスの螺旋 using Plots theta = range(0, 8 * pi, length=500) r = 0.5 * theta plot(theta, r, proj = :polar, label = "Archimedean spiral")
アルキメデスの螺旋
Julia の Plots で Shape 型を使用すると、任意の多角形や領域を描画することができます。Shape([x1, x2, ...], [y1, y2, ...]) で多角形の頂点を指定し、plot(shape) で描画します。plot([shape1, shape2]) のように配列を渡すことで、複数の図形を一度に描画することもできます。なお、Shape はデフォルトで内部を塗りつぶします。
リスト : 多角形の描画 (1) using Plots x = [1, 6, 11, 8.5, 3.5] y = [10, 15, 10, 2, 2] plot(Shape(x, y), ylims=(0, 16))
五角形
リスト : 多角形の描画 (2) using Plots # 四角形の頂点を指定 r1 = Shape([2, 4, 4, 2], [15, 15, 1, 1]) r2 = Shape([4, 6, 6, 4], [15, 15, 1, 1]) r3 = Shape([6, 8, 8, 6], [15, 15, 1, 1]) r4 = Shape([8, 10, 10, 8], [15, 15, 1, 1]) plot([r1, r2, r3, r4], xlims=(0, 12), ylims=(0, 16))
四角形
リスト : 多角形の描画 (3) using Plots # 四角形の頂点を指定 r1 = Shape([2, 4, 4, 2], [15, 15, 1, 1]) r2 = Shape([4, 6, 6, 4], [15, 15, 1, 1]) r3 = Shape([6, 8, 8, 6], [15, 15, 1, 1]) r4 = Shape([8, 10, 10, 8], [15, 15, 1, 1]) # 五角形 x = [1, 6, 11, 8.5, 3.5] y = [10, 15, 10, 2, 2] z = Shape(x, y) plot([r1, r2, r3, r4, z], xlims=(0, 12), ylims=(0, 16), fillalpha = 0.3)
重ね合わせ
Julia の Plots で塗りつぶしを行うには属性 fillrange を使用します。
fillrange に数値 n を指定すると、プロットした曲線と直線 y = n の間を塗りつぶします。fillrange にもう一方の曲線の y の値を配列で渡すと、2 つの曲線の間を塗りつぶします。
リスト : 塗りつぶし (1) using Plots f2(x) = x ^ 3 - 9 * x f3(x) = x ^ 4 - 10 * x ^ 2 + 9 plot(f2, -2, 2, fillrange=0, fillalpha=0.3, label = "x^3 - 9x") plot!(f3, -2, 2, fillrange=-15, fillalpha=0.3, label = "x^4 -10x + 9")
塗りつぶし (1)
リスト : 塗りつぶし (2) using Plots f2(x) = x ^ 3 - 9 * x f3(x) = x ^ 4 - 10 * x ^ 2 + 9 xs = range(2, -2, length=100) ys2 = [f2(x) for x in xs] ys3 = [f3(x) for x in xs] plot(xs, ys2, fillrange=ys3, fillalpha=0.3, label = "x^3 - 9x") plot!(xs, ys3, label = "x^4 -10x + 9")
塗りつぶし (2)
積み上げ面グラフ (Stacked Area Chart) は、折れ線グラフの各線の下側を色付けし、それらを上に積み上げた形式のグラフです。時系列などの変化に沿って、全体の合計値の変化と、各カテゴリーが占める割合 (内訳) の両方を一目で確認することができます。Julia の Plots で積み上げ面グラフを描画するには関数 areaplot () を使います。
areaplot(x, y) の引数 x は x 軸の値を格納した配列、引数 y はデータを格納した行列で、行が積み上げられるデータになります。デフォルトでは縦方向にデータを積み上げます。属性 fillalpha で透明度を、fillcolor で色を指定することができます。
リスト : 積み上げ面グラフ
using Plots
# データの準備
x = 1 : 6
y = [1 2 3; 7 8 9; 4 5 6; 5 2 1; 2 6 8; 3 5 7] # 各行が積み上げられるデータ
# 積層面グラフの作成
areaplot(x, y,
title = "Stacked Area Chart Example",
labels = ["Group A" "Group B" "Group C"],
fillalpha = 0.5)
積み上げ面グラフ
階段グラフ (ステップチャート, Step Line Chart) は、データの値が変化するまで、前の値を水平に維持するグラフです Julia の Plots で階段グラフを描画するには、関数 plot() の属性 linetype に以下の値を指定します。
階段グラフでも、通常のプロットと同様のカスタマイズ (たとえば太さや lw = 2 や色 color = :red など) が可能です。また、マーカー markershape = :circle を追加すると、実際のデータポイントがどこにあるか分かりやすくなります。
リスト : 階段グラフ using Plots default(markershape = :circle, ylims=(0, 12)) x = 1:10 y = [2, 3, 5, 4, 6, 8, 7, 9, 10, 8] p1 = plot(x, y, linetype = :steppre, title="steppre") p2 = plot(x, y, linetype = :stepmid, title="stepmid") p3 = plot(x, y, linetype = :steppost, title="steppost") plot(p1, p2, p3, layout=(3, 1))
ステップチャート
ヒートマップ (heatmap) は、数値データの大小を色の濃淡や色相で表現し、データの分布や強弱を一目で把握できるようにしたグラフです。Plots でヒートマップを描画するには、主に関数 heatmap() を使用します。行列データ (2 次元配列) を渡すと、値の大きさを色の濃淡で表現してくれます。
リスト : ヒートマップ (1) using Plots # ランダムな行列データを作成 data = rand(20, 20) # ヒートマップの描画 heatmap(data, title="Basic Heatmap", xlabel="X-axis", ylabel="Y-axis")
ヒートマップ (1)
x / y 軸の値を指定する場合、第 1 引数に x 軸の範囲、第 2 引数に y 軸の範囲を指定し、第 3 引数に関数 f(x, y) を渡します。
リスト : ヒートマップ (2) using Plots x = -5 : 0.25 : 5 y = -10 : 0.5 : 10 f(x, y) = sqrt(x^2 + y^2) heatmap(x, y, f)
ヒートマップ (2)
第 3 引数は関数ではなく、次のような 1 次元配列でもかまいません。
リスト : 関数の値を 1 次元配列で渡す z = [f(i, j) for j in y, i in x] heatmap(x, y, z)
よく用いられる属性を以下に示します。
リスト : ヒートマップ (3) using Plots x = 1 : 0.1 : 10 y = 1 : 0.1 : 10 z = [sin(xi) * cos(yi) for xi in x, yi in y] heatmap(x, y, z, xlabel="X", ylabel="Y", c=:viridis)
ヒートマップ (3)
Julia の Plots を使用して等高線図を描くには、主に関数 contour() または関数 contourf() を使用します。contour() は線のみ、contourf() は塗りつぶしを行います。基本的な使い方は簡単で、関数 f(x, y) を定義し、x 軸と y 軸の範囲を指定してプロットします。
リスト : 等高線図 (1) using Plots x = range(-6, 6, length=100) y = range(0, 12, length=100) f(x, y) = sin(x) + cos(y) # 線のみ p1 = contour(x, y, f, title="contour") # 塗りつぶし p2 = contourf(x, y, f, title="contourf") plot(p1, p2)
等高線図 (1)
主な属性を以下に示します。
リスト : 等高線図 (2)
using Plots
x = y = range(-2, 2, length=100)
f(x, y) = x^2 + y^2
contour(x, y, f,
levels = [1,2,3,4,5,6,7],
contour_labels = true, # 数値ラベルを表示
c = :thermal, # 配色
lw = 2, # 線の太さ
colorbar_title = "Z value"
)
等高線図 (2)
円が歪んで楕円になる場合、属性 ratio=:equal または aspect_ratio=:equal を追加します。これで x 軸と y 軸の単位長さが等しくなります。
リスト : 等高線図 (3)
using Plots
x = range(-3, 3, length=100)
y = range(-2, 2, length=100)
f(x, y) = x^2 + y^2
contour(x, y, f,
levels = [1,2,3,4,5,6,7],
contour_labels = true, # 数値ラベルを表示
c = :thermal, # 配色
lw = 2, # 線の太さ
colorbar_title = "Z value",
ratio=:equal
)
等高線図 (3)
Julia の Plots で 3 次元折れ線グラフ (3D Line Plot) を描画するには、関数 plot() に 3 つの引数 (x, y, z) を渡すか、明示的に関数 plot3d(x, y, z) を使用します。3 次元の散布図は関数 scatter3d(x, y, z) を使うと簡単です。引数 x, y, z は各座標の値を格納した配列です。
リスト : 3D Line Plot using Plots z = range(0, 10 * pi, length=500) x = sin.(z) y = cos.(z) plot3d(x, y, z, title="3D Line Plot", lw=2)
3D Line Plot
リスト : 3D Scatter using Plots x, y, z = rand(50), rand(50), rand(50) scatter3d(x, y, z, markersize=3, title="3D Scatter")
3D Scatter
Julia の Plots で 3 次元曲面を描画するには、主に以下の関数を使用します。
2 変数関数 z = f(x, y) を可視化する場合、x 軸と y 軸の範囲を指定し、引数 z に関数 f を渡すのが一般的です。引数の仕様は contour() や heatmap() と同じです。ようするに、contour や heatmap のかわりに上記関数を使うと、3 次元曲面を表示することができます。
リスト : 3 次元の曲面 (1) using Plots x = range(-3, 3, length=100) y = range(-3, 3, length=100) f(x, y) = sqrt(x^2 + y^2) surface(x, y, f, title="3D Surface")
3D Surface
リスト : 3 次元の曲面 (2) using Plots x = range(-3, 3, length=30) y = range(-3, 3, length=30) f(x, y) = sqrt(x^2 + y^2) # ワイヤーフレームの描画 wireframe(x, y, f, title="3D Wireframe")
3D Wireframe
リスト : 3 次元の曲面 (3) using Plots x = range(-6, 6, length=30) y = range(0, 12, length=30) f(x, y) = sin(x) + cos(y) # 線のみ p1 = wireframe(x, y, f, title="wireframe") # 塗りつぶし p2 = surface(x, y, f, title="surface", colorbar=:none) plot(p1, p2)
3 次元の曲面