Plotting in Julia

We will use the PyPlot package to plot with Julia. This notebook has a few examples to get you started. The PyPlot.jl site has excellent documentation for plotting.

Loading the PyPlot module may take a few seconds.

In general, all of the arguments, including keyword arguments, are exactly the same as in Python. (With minor translations, of course, e.g. Julia uses true and nothing instead of Python's True and None.)

The full matplotlib.pyplot API is far too extensive to describe here; see the matplotlib.pyplot documentation for more information. The Matplotlib version number is returned by PyPlot.version.


In [1]:
using PyPlot


INFO: Precompiling module PyPlot...

In [2]:
x = linspace(0,2*pi,1000); y = sin(3*x + 4*cos(2*x));
plot(x, y, color="red", linewidth=2.0, linestyle="--")
title("A sinusoidally modulated sinusoid")


Out[2]:
PyObject <matplotlib.text.Text object at 0x7f808e38f810>

In [3]:
# Draw (x, y) points
figure(figsize=(5, 5))
θ = collect(0:0.1:2π)
plot(0,0,"b.")
plot(cos(θ), sin(θ), "r.")
plot(0.5cos(θ), 0.5sin(θ), "g.")


WARNING: [a] concatenation is deprecated; use collect(a) instead
 in depwarn at deprecated.jl:73
 in oldstyle_vcat_warning at ./abstractarray.jl:29
 in vect at abstractarray.jl:32
 in include_string at loading.jl:282
 in execute_request_0x535c5df2 at /opt/julia_packages/.julia/v0.4/IJulia/src/execute_request.jl:183
 in eventloop at /opt/julia_packages/.julia/v0.4/IJulia/src/IJulia.jl:143
 in anonymous at task.jl:447
while loading In[3], in expression starting on line 3
Out[3]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f808c71d8d0>

In [4]:
# Draw a histogram

y = randn(10^6)
plt[:hist](y, 50)     # We use plt.hist, because it conflicts with the built-in hist


Out[4]:
([2.0,0.0,4.0,5.0,11.0,33.0,91.0,152.0,268.0,535.0  …  836.0,486.0,215.0,120.0,68.0,32.0,7.0,5.0,0.0,3.0],[-5.07738,-4.8775,-4.67761,-4.47772,-4.27783,-4.07794,-3.87806,-3.67817,-3.47828,-3.27839  …  3.11801,3.3179,3.51779,3.71767,3.91756,4.11745,4.31734,4.51723,4.71711,4.917],Any[PyObject <matplotlib.patches.Rectangle object at 0x7f808bf0ad90>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bf17410>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bf17910>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bf17f90>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bea5650>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bea5cd0>,PyObject <matplotlib.patches.Rectangle object at 0x7f808beb1390>,PyObject <matplotlib.patches.Rectangle object at 0x7f808beb1a10>,PyObject <matplotlib.patches.Rectangle object at 0x7f808beb1f10>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bebb750>  …  PyObject <matplotlib.patches.Rectangle object at 0x7f808be4a6d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f808be4ad50>,PyObject <matplotlib.patches.Rectangle object at 0x7f808be55410>,PyObject <matplotlib.patches.Rectangle object at 0x7f808be55a90>,PyObject <matplotlib.patches.Rectangle object at 0x7f808be55f90>,PyObject <matplotlib.patches.Rectangle object at 0x7f808be627d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f808be62e50>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bdec510>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bdecb90>,PyObject <matplotlib.patches.Rectangle object at 0x7f808bdf80d0>])

In [5]:
# Draw a stacked bar chart

N = 5
menMeans   = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd     = (2, 3, 4, 1, 2)
womenStd   = (3, 5, 2, 3, 3)
ind = [1:N]    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = bar(ind, menMeans,   width, color="r", yerr=womenStd)
p2 = bar(ind, womenMeans, width, color="y", bottom=menMeans, yerr=menStd)

ylabel("Scores")
title("Scores by group and gender")
xticks(ind+width/2., ("G1", "G2", "G3", "G4", "G5") )
yticks([0:10:81])
legend( (p1[1], p2[1]), ("Men", "Women") )


Out[5]:
PyObject <matplotlib.legend.Legend object at 0x7f808bd69390>

In [6]:
# Plot a random surface

surf(rand(30,40))


Out[6]:
PyObject <mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x7f808bc90c10>