PyPlot and Plots

黒木玄

2018-01-29, 2018-09-13

PyPlot.jl によるプロットと Plots.jl によるプロットを比較してみる.

注意

import PyPlotusing PyPlot に変えれば PyPlot. を省略できる. これは Plots についても同様である. しかし, using PyPlotusing Plots は両立しないので注意せよ.

警告: 筆者は特に Plots.jl の経験が浅く, よく理解していない部分が多数ある.

ファイルの表示用函数

In [1]:
using Base64
using Pkg
using Random
endof(a) = lastindex(a)
linspace(start, stop, length) = range(start, stop=stop, length=length)
const srand = Random.seed!

displayfile(mime, file) = open(file) do f
    base64 = base64encode(f)
    display("text/html", """<img src="data:$mime;base64,$base64">""")
end

using Distributions
import PyPlot
import Plotly
import Plots

PyPlot.plot と Plots.plot

In [2]:
import PyPlot
PyPlot.plt[:style][:use]("default")
PyPlot.rc("figure", titlesize=10)
PyPlot.rc("lines", linewidth=1)
PyPlot.rc("lines", markersize=5)
PyPlot.rc("axes", grid=true)
PyPlot.rc("axes", labelsize=10)
PyPlot.rc("grid", linestyle=":")
PyPlot.rc("xtick", labelsize=8)
PyPlot.rc("ytick", labelsize=8)
PyPlot.rc("legend", fontsize=8)

srand(12345)
N = 20
PyPlot.figure(figsize=(5,5))
PyPlot.plot(rand(N), rand(N), label="1", marker="o")
PyPlot.plot(rand(N), rand(N), label="2", marker="^", ls="--")
PyPlot.plot(rand(N), rand(N), label="3", marker="s", ls="-.")
PyPlot.legend()
PyPlot.xlim(-0.05, 1.05)
PyPlot.ylim(-0.05, 1.05)
PyPlot.xlabel("x")
PyPlot.ylabel("y")
PyPlot.axes()[:set_aspect]("equal")
PyPlot.title("2-dimensional uniform distribution")
Out[2]:
PyObject Text(0.5,1,'2-dimensional uniform distribution')
In [3]:
import Plots
Plots.reset_defaults()
Plots.pyplot(
    legend=false,
    titlefont=Plots.font("sans-serif", 12),
    legendfont=Plots.font("sans-serif", 8),
    guidefont=Plots.font("sans-serif", 10),
    tickfont=Plots.font("sans-serif", 8),
    markersize=5,
    markerstrokewidth=0,
)

srand(12345)
N = 20
Plots.plot(size=(450, 450))
Plots.plot!(rand(N), rand(N), label="1", marker=:circle)
Plots.plot!(rand(N), rand(N), label="2", marker=:utriangle, ls=:dash)
Plots.plot!(rand(N), rand(N), label="3", marker=:square,    ls=:dashdot)
Plots.plot!(legend=true)
Plots.plot!(xlim=(-0.05, 1.05))
Plots.plot!(ylim=(-0.05, 1.05))
Plots.plot!(xlabel="x")
Plots.plot!(ylabel="y")
Plots.plot!(aspect_ratio=1)
Plots.title!("2-dimensional uniform distribution")
Out[3]:
In [4]:
import Plots
Plots.reset_defaults()
Plots.gr(
    legend=false,
    titlefont=Plots.font("sans-serif", 12),
    legendfont=Plots.font("sans-serif", 8),
    guidefont=Plots.font("sans-serif", 10),
    tickfont=Plots.font("sans-serif", 8),
    markersize=5,
    markerstrokewidth=0,
)

srand(12345)
N = 20
Plots.plot(size=(450, 450))
Plots.plot!(rand(N), rand(N), label="1", marker=:circle)
Plots.plot!(rand(N), rand(N), label="2", marker=:utriangle, ls=:dash)
Plots.plot!(rand(N), rand(N), label="3", marker=:square,    ls=:dashdot)
Plots.plot!(legend=true)
Plots.plot!(xlim=(-0.05, 1.05))
Plots.plot!(ylim=(-0.05, 1.05))
Plots.plot!(xlabel="x")
Plots.plot!(ylabel="y")
Plots.plot!(aspect_ratio=1)
Plots.title!("2-dimensional uniform distribution")
Out[4]:
0.00 0.25 0.50 0.75 1.00 0.00 0.25 0.50 0.75 1.00 2-dimensional uniform distribution x y 1 2 3
In [5]:
import Plots
Plots.reset_defaults()
#Plots.plotlyjs(
Plots.plotly(
    legend=false,
    titlefont=Plots.font("sans-serif", 12),
    legendfont=Plots.font("sans-serif", 8),
    guidefont=Plots.font("sans-serif", 10),
    tickfont=Plots.font("sans-serif", 8),
    markersize=5,
    markerstrokewidth=0,
)

srand(12345)
N = 20
Plots.plot(size=(500,420))
Plots.plot!(rand(N), rand(N), label="1", marker=:circle)
Plots.plot!(rand(N), rand(N), label="2", marker=:utriangle, ls=:dash)
Plots.plot!(rand(N), rand(N), label="3", marker=:square,    ls=:dashdot)
Plots.plot!(legend=true)
Plots.plot!(xlim=(-0.05, 1.05))
Plots.plot!(ylim=(-0.05, 1.05))
Plots.plot!(xlabel="x")
Plots.plot!(ylabel="y")
Plots.plot!(aspect_ratio=0.85)
Plots.title!("2-dimensional uniform distribution")
Out[5]:

確率密度スケールのヒストグラム

In [6]:
using KernelDensity

srand(2018)
X = vcat(2 .+ 2*randn(6000), -3 .+ 0.5*randn(4000))
ik = InterpKDE(kde(X))
pdfik(x) = pdf(ik, x)
x = linspace(minimum(X), maximum(X), 401)
y = pdfik.(x)

import PyPlot
PyPlot.plt[:style][:use]("default")
PyPlot.rc("figure", titlesize=10)
PyPlot.rc("lines", linewidth=1)
PyPlot.rc("axes", grid=true)
PyPlot.rc("axes", labelsize=10)
PyPlot.rc("grid", linestyle=":")
PyPlot.rc("xtick", labelsize=8)
PyPlot.rc("ytick", labelsize=8)
PyPlot.rc("legend", fontsize=10)

PyPlot.figure(figsize=(5, 3.5))
PyPlot.plt[:hist](X, normed=true, bins=100, alpha=0.3, label="sample")
PyPlot.plot(x, y, label="kde")
PyPlot.legend()
PyPlot.xlabel("x")
PyPlot.ylabel("probability density")
PyPlot.title("sample of mixture normal distribution")
Out[6]:
PyObject Text(0.5,1,'sample of mixture normal distribution')
In [7]:
using KernelDensity

srand(2018)
X = vcat(2 .+ 2*randn(6000), -3 .+ 0.5*randn(4000))
ik = InterpKDE(kde(X))
pdfik(x) = pdf(ik, x)
x = linspace(minimum(X), maximum(X), 401)
y = pdfik.(x)

import Plots
Plots.reset_defaults()
Plots.pyplot(
    legend=false,
    titlefont=Plots.font("sans-serif", 12),
    legendfont=Plots.font("sans-serif", 10),
    guidefont=Plots.font("sans-serif", 10),
    tickfont=Plots.font("sans-serif", 8),
)

Plots.plot(size=(450, 315))
Plots.histogram!(X, normed=true, bins=100, alpha=0.2, label="sample", legend=true)
Plots.plot!(x, y, label="kde", legend=true, ylims=(0, 0.33))
Plots.plot!(xlabel="x")
Plots.plot!(ylabel="probability density")
Plots.title!("sample of mixture normal distribution")
Out[7]:
In [8]:
using KernelDensity

srand(2018)
X = vcat(2 .+ 2*randn(6000), -3 .+ 0.5*randn(4000))
ik = InterpKDE(kde(X))
pdfik(x) = pdf(ik, x)
x = linspace(minimum(X), maximum(X), 401)
y = pdfik.(x)

import Plots
Plots.reset_defaults()
Plots.gr(
    legend=false,
    titlefont=Plots.font("sans-serif", 12),
    legendfont=Plots.font("sans-serif", 10),
    guidefont=Plots.font("sans-serif", 10),
    tickfont=Plots.font("sans-serif", 8),
)

Plots.plot(size=(450, 315))
Plots.histogram!(X, normed=true, bins=100, alpha=0.2, label="sample", legend=true)
Plots.plot!(x, y, label="kde", legend=true, ylims=(0, 0.33))
Plots.plot!(xlabel="x")
Plots.plot!(ylabel="probability density")
Plots.title!("sample of mixture normal distribution")
Out[8]:
-5 0 5 10 0.0 0.1 0.2 0.3 sample of mixture normal distribution x probability density sample kde

2次元サンプルに密度で色を付ける

In [9]:
using KernelDensity

srand(2018)
n = 2^10
X, Y = randn(n), randn(n)
X, Y = 2X, X.^2 + X + 1.2Y

ik = InterpKDE(kde((X,Y)))
pdfik(x,y) = pdf(ik,x,y)
c = pdfik.(X, Y)

x = linspace(minimum(X), maximum(X), 201)
y = linspace(minimum(Y), maximum(Y), 201)
z = pdfik.(x', y)

import PyPlot
PyPlot.plt[:style][:use]("default")
PyPlot.rc("lines", markersize=2)
PyPlot.rc("grid", linestyle=":")
PyPlot.rc("axes", facecolor="gray")

PyPlot.figure(figsize=(8,3.4))
PyPlot.subplot(121)
PyPlot.scatter(X, Y, c=c, cmap="CMRmap")
PyPlot.colorbar()
PyPlot.title("scatter")
PyPlot.subplot(122)
PyPlot.pcolormesh(x, y, z, cmap="CMRmap")
PyPlot.colorbar()
PyPlot.title("pcolormesh")
PyPlot.tight_layout()

PyPlot.savefig("PyPlot_scatter_pcolormesh.png")
displayfile("image/png", "PyPlot_scatter_pcolormesh.png")
In [10]:
using KernelDensity

srand(2018)
n = 2^10
X, Y = randn(n), randn(n)
X, Y = 2X, X.^2 + X + 1.2Y

ik = InterpKDE(kde((X,Y)))
pdfik(x,y) = pdf(ik,x,y)
c = pdfik.(X, Y)

x = linspace(minimum(X), maximum(X), 201)
y = linspace(minimum(Y), maximum(Y), 201)
z = pdfik.(x', y)

import Plots
Plots.reset_defaults()
Plots.pyplot(
    legend=false,
    markersize=3,
    markerstrokewidth=0,
    background_color_inside="gray",
)

p1 = Plots.scatter(X, Y, zcolor=c, color=:thermal, colorbar=true, title="scatter")
p2 = Plots.heatmap(x, y, z, color=:thermal, colorbar=true, title="heatmap")
Plots.plot(p1, p2, size=(720, 320))
Plots.savefig("Plots_pyplot_scatter_heatmap.png")
displayfile("image/png", "Plots_pyplot_scatter_heatmap.png")
In [11]:
using KernelDensity

srand(2018)
n = 2^10
X, Y = randn(n), randn(n)
X, Y = 2X, X.^2 + X + 1.2Y

ik = InterpKDE(kde((X,Y)))
pdfik(x,y) = pdf(ik,x,y)
c = pdfik.(X, Y)

x = linspace(minimum(X), maximum(X), 201)
y = linspace(minimum(Y), maximum(Y), 201)
z = pdfik.(x', y)

import Plots
Plots.reset_defaults()
Plots.gr(
    legend=false,
    markersize=3,
    markerstrokewidth=0,
    background_color_inside="gray",
)

p1 = Plots.scatter(X, Y, zcolor=c, color=:thermal, colorbar=true, title="scatter")
p2 = Plots.heatmap(x, y, z, color=:thermal, colorbar=true, title="heatmap")
Plots.plot(p1, p2, size=(800, 320))
Plots.savefig("Plots_gr_scatter_heatmap.png")
In [12]:
Plots.plot(p1, p2, size=(800, 320))
Out[12]:
-6 -4 -2 0 2 4 6 -2.5 0.0 2.5 5.0 7.5 10.0 12.5 scatter 0.01 0.02 0.03 0.04 0.05 0.06 -5.0 -2.5 0.0 2.5 5.0 -2 0 2 4 6 8 10 12 heatmap