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 [9]:
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[9]:
PyObject <matplotlib.text.Text object at 0x7fd209a8aa10>

In [10]:
# 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[10]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7fd209981150>

In [11]:
# Draw a histogram

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


Out[11]:
([4.0,6.0,9.0,27.0,59.0,91.0,217.0,366.0,641.0,1100.0  …  1115.0,644.0,380.0,189.0,96.0,54.0,27.0,9.0,3.0,3.0],[-4.67439,-4.48748,-4.30058,-4.11367,-3.92677,-3.73986,-3.55296,-3.36605,-3.17915,-2.99224  …  2.98871,3.17561,3.36252,3.54942,3.73633,3.92323,4.11014,4.29704,4.48395,4.67085],{PyObject <matplotlib.patches.Rectangle object at 0x7fd2091715d0>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209171bd0>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209180290>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209180910>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209180f90>,PyObject <matplotlib.patches.Rectangle object at 0x7fd20910d650>,PyObject <matplotlib.patches.Rectangle object at 0x7fd20910dcd0>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209117390>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209117a10>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209117f10>  …  PyObject <matplotlib.patches.Rectangle object at 0x7fd2090a9f50>,PyObject <matplotlib.patches.Rectangle object at 0x7fd2090b7610>,PyObject <matplotlib.patches.Rectangle object at 0x7fd2090b7c90>,PyObject <matplotlib.patches.Rectangle object at 0x7fd2090c4350>,PyObject <matplotlib.patches.Rectangle object at 0x7fd2090c49d0>,PyObject <matplotlib.patches.Rectangle object at 0x7fd2090c4ed0>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209050710>,PyObject <matplotlib.patches.Rectangle object at 0x7fd209050d90>,PyObject <matplotlib.patches.Rectangle object at 0x7fd20905d450>,PyObject <matplotlib.patches.Rectangle object at 0x7fd20905dad0>})

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

In [13]:
# Plot a random surface

surf(rand(30,40))


Out[13]:
PyObject <mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x7fd208e8be10>