Julia has several high quality registered plotting packages:
Browse the manuals (20 min):
The reader should be able to use some of the features of the above packages.
Plotting packages are rather complex and depend on additional software, so it is advised to execute corresponding
Pkg.add()
commands in terminal mode.
Also, plotting packges frequently use same (obvious) names for plot functions. When using more than one package in a Julia session, the functions need to be called by specifying the package, as well.
We shall ilustrate the packages on several numerical examples, which also give the flavor of Julia.
We compute and plot:
We shall use the registered package SpecialMatrices.jl.
In [1]:
# Pkg.add("SpecialMatrices"); Pkg.checkout("SpecialMatrices")
using Winston
using SpecialMatrices
using Polynomials
In [2]:
# Number of intervals
n=5
# n+1 points
t=[1.0,2,4,5,8,9]
y=[-1.0,1,4,0,2,6]
# Computation
h=t[2:end]-t[1:end-1]
b=(y[2:end]-y[1:end-1])./h
v=6*(b[2:end]-b[1:end-1])
H=SymTridiagonal(2*(h[1:end-1]+h[2:end]),h[2:end-1])
Out[2]:
In [3]:
z=H\v
z=[0;z;0]
Out[3]:
In [4]:
# Define the splines
B=b-(z[2:end]-z[1:end-1]).*h/6
S=Array{Any}(n)
S=[x-> y[i]-z[i]*h[i]^2/6+B[i]*(x-t[i])+z[i]*(t[i+1]-x)^3/
(6*h[i])+z[i+1]*(x-t[i])^3/(6*h[i]) for i=1:n]
Out[4]:
In [5]:
# Define the points to plot
lsize=200
x=linspace(t[1],t[end],lsize)
zSpline=Array{Float64}(lsize)
for i=1:lsize
for k=1:n
if x[i]<=t[k+1]
zSpline[i]=S[k](x[i])
break
end
end
end
In [6]:
# Plot
Winston.plot(t,y,"r*",x,zSpline,"b")
Winston.title("Natural cubic spline")
Winston.xlabel("x")
Winston.ylabel("y")
Out[6]:
In [7]:
# Standard interpolating polynomial
A=Vandermonde(t)
p=Poly(full(A)\y)
yPoly=p(x)
Winston.plot(t,y,"r*",x,zSpline,"b",x,yPoly,"g")
Out[7]:
We shall illustrate Gadfly with two examples:
N.B. Gadfly plots can be nicely zoomed in or out. Variety of ODE solvers can be found in the package ODE.jl
Derivative of a function can be:
In [8]:
# Pkg.add("ForwardDiff")
using ForwardDiff
using Gadfly
In [9]:
f(x)=exp(x)-x-5.0/4
Gadfly.plot([f,x->ForwardDiff.derivative(f,x)],-2.0,2.0,
Guide.yticks(ticks=[-1.0,-0.5,0.0,0.5,1.0]))
Out[9]:
In [10]:
# Euler's method
function myEuler{T,T1}(f::Function,y0::T,x::T1)
h=x[2]-x[1]
y=Array{T}(length(x))
y[1]=y0
for i=2:length(x)
y[i]=y[i-1]+h*f(x[i-1],y[i-1])
end
y
end
Out[10]:
In [11]:
# n subintervals on the interval [0,1]
n=30
x=linspace(0,1,n+1)
f1(x,y)=x+y
y=myEuler(f1,1.0,x)
Out[11]:
In [12]:
# We can plot functions and data sets (points) using layers
solution(x)=2*exp(x)-x-1
Gadfly.plot(layer(solution,0,1),
layer(x=x,y=y,Geom.point, Geom.line,
Theme(default_color=colorant"orange")))
Out[12]:
In [13]:
# Or, with a different geometry
Gadfly.plot(layer(solution,0,1),
layer(x=x,y=y,Geom.point, Geom.bar,
Theme(default_color=colorant"orange")))
Out[13]:
We shall illustrate PyPlot with two examples:
The solutions of the system
\begin{align*} 2(x_1+x_2)^2-(x_1-x_2)^2&=8\\ 5x_1^2+(x_2-3)^2&=9 \end{align*}are
\begin{align*} S_1&=(-1.183467003241957,1.5868371427229244),\\ S_2&=(1,1). \end{align*}Let us plot the surfaces. We shall use the function meshgrid()
from
https://fossies.org/linux/julia/examples/ndgrid.jl.
In [14]:
using PyPlot
In [15]:
function meshgrid{T}(vx::AbstractVector{T}, vy::AbstractVector{T})
m, n = length(vy), length(vx)
vx = reshape(vx, 1, n)
vy = reshape(vy, m, 1)
(repmat(vx, m, 1), repmat(vy, 1, n))
end
Out[15]:
In [16]:
# Define the system
f(x,y)=[2(x+y).^2+(x-y).^2-8,5*x.^2+(y-3).^2-9]
Out[16]:
In [17]:
# Prepare the mexhgrid manually
gridsize=100
X=linspace(-2,3,gridsize)
Y=linspace(-2,2,gridsize)
U=meshgrid(X,Y)
Z=f(U[1],U[2])
Out[17]:
In [18]:
# Plot
PyPlot.surf(U[1],U[2],Z[1])
PyPlot.surf(U[1],U[2],Z[2],color="red")
PyPlot.xlabel("x")
PyPlot.ylabel("y")
Out[18]:
Let us plot the contours at $z=0$:
In [19]:
C1=contour(U[1],U[2],Z[1],levels=[0])
C2=contour(U[1],U[2],Z[2],levels=[0],colors="red")
clabel(C1,inline=1, fontsize=10)
clabel(C2,inline=1, fontsize=10)
PyPlot.xlabel("x")
PyPlot.ylabel("y")
Out[19]:
In [20]:
using SymPy
In [21]:
# Define the parameters
C=0.25
σ=1/3
# Define symbolic variables
@vars x y
Out[21]:
In [22]:
SymPy.plot_implicit(Eq(y*x^σ,C*exp(y+σ*x)),(x,0.5,2),(y,0.5,1.5))
Out[22]:
In [ ]: