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: Loading help data...

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 0x7f3790bdc310>

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


Out[3]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f37515b47d0>

In [5]:
# Draw a histogram

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


Out[5]:
([2.0,2.0,2.0,7.0,17.0,46.0,84.0,183.0,346.0,592.0  …  1333.0,776.0,416.0,229.0,114.0,50.0,20.0,14.0,5.0,1.0],[-4.91619,-4.72466,-4.53313,-4.3416,-4.15007,-3.95855,-3.76702,-3.57549,-3.38396,-3.19243  …  2.93652,3.12805,3.31958,3.51111,3.70264,3.89417,4.0857,4.27723,4.46876,4.66029],{PyObject <matplotlib.patches.Rectangle object at 0x7f3750602f50>,PyObject <matplotlib.patches.Rectangle object at 0x7f3750612590>,PyObject <matplotlib.patches.Rectangle object at 0x7f3750612c10>,PyObject <matplotlib.patches.Rectangle object at 0x7f375059f2d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f375059f950>,PyObject <matplotlib.patches.Rectangle object at 0x7f375059ffd0>,PyObject <matplotlib.patches.Rectangle object at 0x7f37505ab690>,PyObject <matplotlib.patches.Rectangle object at 0x7f37505abd10>,PyObject <matplotlib.patches.Rectangle object at 0x7f37505b83d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f37505b8a50>  …  PyObject <matplotlib.patches.Rectangle object at 0x7f37505534d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f3750553b50>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504df210>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504df890>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504dff10>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504eb5d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504ebc50>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504f7310>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504f7990>,PyObject <matplotlib.patches.Rectangle object at 0x7f37504f7e90>})

In [6]:
# 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[6]:
PyObject <matplotlib.legend.Legend object at 0x7f37503fc9d0>

In [7]:
# Plot a random surface

surf(rand(30,40))


Out[7]:
PyObject <mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x7f3751546110>