2020-07-19
Juliaでグラフ作成
Juliaのグラフ描画パッケージのPlots.jl
の使い方をまとめる.
バージョン
Julia v1.1.0
Plots v0.26.3
インストール
pyplotをバックエンドとして使う例を書いていく.
pkg> add Plots
pkg> add PyPlot
AnacondaがインストールされていなくてPyPlot
をbuildできない場合は
pkg> build PyCall
とすることで自動的にAnacondaがインストールされる.
参考
基本的なplot方法
まず,Plots
モジュールをusing
して,pyplot()
でバックエンドとしてPyPlot
を使用することを明示する.
using Plots
pyplot()
line
点を線でつないで表示する
using Plots
pyplot()
x = 1:10
y = sin.(x)
plot(x, y)
scatter
点をそのままプロット
using Plots
pyplot()
x = 1:10
y = sin.(x)
scatter(x, y)
contour
等高線をプロットする.
変数 | 内容 | Array size |
---|---|---|
vx | x軸方向のグリッド vx = [0, 1, ..., 10] | (11, ) |
vy | y軸方向のグリッド vy = [0, 1, ..., 10] | (11, ) |
z | 点(x, y)における値 z = f(x, y) | (11, 11) |
using Plots
using Base.Iterators
pyplot()
vx = 0:10
vy = 0:10
z = map(product(vx, vy)) do (x, y)
(x - 5)^2 + (y - 5)^2
end
contour(vx, vy, z)
Base.Iterators
のproduct
を使うことでvx
, vy
を使った二次元グリッドの配列を作成できる.各点(x, y)
における値を計算してzという配列を作る.
contourf
contour
同様.等高線の間の領域が塗り分けられる.
using Plots
using Base.Iterators
pyplot()
vx = 0:10
vy = 0:10
z = map(product(vx, vy)) do (x, y)
(x - 5)^2 + (y - 5)^2
end
contourf(vx, vy, z)
surface
z = f(x, y)
の平面を描画する.入力しているvx, vy, z
はcontour
と同様の配列.
using Plots
using Base.Iterators
pyplot()
vx = range(0, stop=1, length=100)
vy = range(0, stop=1, length=100)
z = map(product(vx, vy)) do (x, y)
(x - 0.5)^2 + (y - 0.5)^2
end
surface(vx, vy, z)
heatmap
heatmap図.z = f(x, y)
を平面で表示する.
using Plots
using Base.Iterators
pyplot()
vx = range(0, stop=1, length=100)
vy = range(0, stop=1, length=100)
z =map(product(vx, vy)) do (x, y)
(x - 0.5)^2 + (y - 0.5)^2
end
heatmap(vx, vy, z)
基本的なOption
このセクションでは公式のドキュメントhttp://docs.juliaplots.org/latest/examples/pyplot/を参考にしている.
Plots.jl
ではplot
やscatter
,contour
など基本的に共通でキーワード引数を追加することでグラフのラベルなどを変更できるのでいくつかの使用例を示します.
title
グラフのタイトルを追加する
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, title="This is Title")
xlabel/ylabel
x軸のラベル
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, xlabel="x-axis")
legend
グラフのlegendの文字列を指定する.
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, label="test legend")
xlims/ylims
グラフの表示範囲を指定する.
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, xlims=(0.3, 0.5))
ylims
も同様に可能.
仕様なのかはわからないがxlims=(0.3, 0.5)
とTuple
で渡すと綺麗にその範囲だけ表示されるが,xlims=[0.3, 0.5]
とArray
で渡すと余分に広く表示される.
xticks/yticks
x軸のticksの変更.x軸の0, 1/3, 2/3, 1
でラベル"Γ", "K", "M", "Γ"
を表示する.
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, xticks=([0, 1/3, 2/3, 1], ["Γ", "K", "M", "Γ"]))
数字を表示したいだけの場合はxticks
にx座標の配列のみを与える.
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, xticks=[0, 1/3, 2/3, 1])
その他,yticks
とzticks
も同様に可能.
line style
線の種類を変更する
using Plots
pyplot()
x = range(0, stop=1, length=30)
y = sin.(x*2pi)
plot(x, y, linestyle=:dash)
http://docs.juliaplots.org/latest/examples/pyplot/#line-styles
marker
マーカーの種類を変更する
using Plots
pyplot()
x = range(0, stop=1, length=30)
y = sin.(x*2pi)
plot(x, y, marker=:rect)
markerの種類は公式ドキュメント
http://docs.juliaplots.org/latest/examples/pyplot/#marker-types
background_color
グラフの背景色を変更する.RGBA
でキーワード引数background_color
に与える.下の例Alpha=0なので背景を透過して保存したいときに使える.
background_color=RGBA{Float64}(0, 0, 0, 0)
あるいは以下のように:black
, :white
などのSymbolで設定することができる.
background_color=:black
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, background_color=:pink)
framestyle
図をボーダーラインで囲む
framestyle = :box
using Plots
pyplot()
x = range(0, stop=1, length=100)
y = sin.(x*2pi)
plot(x, y, framestyle=:box)
フレームスタイルの一覧: http://docs.juliaplots.org/latest/examples/pyplot/#framestyle
グリッド線を消す
xgrid
, ygrid
に:none
を指定するとグリッド線を消せる.
using Plots
pyplot()
x = 1:10
y = rand(10)
plot(x, y1, xgrid=:none, ygrid=:none)
線の種類や太さなども変更可能
http://docs.juliaplots.org/latest/examples/pyplot/#magic-grid-argument
軸を反転して表示
using Plots
pyplot()
x = 1:10
y = sin.(x)
plot(x, y, xaxis=:flip)
複数プロット
複数プロットは配列で引数に与えればよい.その他のオプションも配列で渡すことで,個別にオプションを適用可能.
using Plots
pyplot()
x = 1:10
y1 = rand(10)
y2 = rand(10)
plot([x, x], [y1, y2])
colormap
surface
やcontour
などでプロットした場合のcolormap
を変更する.color=:viridis
のようにキーワード引数を追加する.キーワード引数color
はline plotなどの場合はlineの色が変わる.
using Plots
using Base.Iterators
pyplot()
vx = range(0, stop=1, length=100)
vy = range(0, stop=1, length=100)
z =map(product(vx, vy)) do (x, y)
(x - 0.5)^2 + (y - 0.5)^2
end
heatmap(vx, vy, z, color=:viridis)
色の一覧は公式のドキュメントで確認できる
http://docs.juliaplots.org/latest/colors/
colorbarを消す
colorbarを消す.バックエンドPyPlot
の場合は位置の指定はサポートされていないようで,:top
などのシンボルは働かない.
using Plots
using Base.Iterators
pyplot()
vx = range(0, stop=1, length=100)
vy = range(0, stop=1, length=100)
z =map(product(vx, vy)) do (x, y)
(x - 0.5)^2 + (y - 0.5)^2
end
heatmap(vx, vy, z, colorbar=:none)
colorbarのタイトル
colorbarのタイトルを追加する
using Plots
using Base.Iterators
pyplot()
vx = range(0, stop=1, length=100)
vy = range(0, stop=1, length=100)
z =map(product(vx, vy)) do (x, y)
(x - 0.5)^2 + (y - 0.5)^2
end
heatmap(vx, vy, z, colorbar_title="HOGE HOGE")
clim
colorbarの範囲に制限をかける
using Plots
using Base.Iterators
pyplot()
vx = range(0, stop=1, length=100)
vy = range(0, stop=1, length=100)
z =map(product(vx, vy)) do (x, y)
(x - 0.5)^2 + (y - 0.5)^2
end
heatmap(vx, vy, z, clim=(0.1, 0.3))
フォントの変更
フォント変更の仕方.xticksfontfamily
などのキーワードで指定できる.
using Plots
using Base.Iterators
pyplot()
vx = range(0, stop=1, length=100)
vy = range(0, stop=1, length=100)
z =map(product(vx, vy)) do (x, y)
(x - 0.5)^2 + (y - 0.5)^2
end
heatmap(vx, vy, z, titlefontfamily="Yu Gothic", title="日本語のタイトル")
使えるフォントはhttps://qiita.com/driller/items/5569871c6cb6bf7d69d4を参考にPythonで確認した.
カテゴリー