PGFPlots

This library uses the LaTeX package pgfplots to produce plots. It integrates with IJulia, outputting SVG images to the notebook.

Installation


In [ ]:
Pkg.add("Images")
Pkg.clone("https://github.com/sisl/PGFPlots.jl")
Pkg.clone("https://github.com/sisl/TikzPictures.jl")

In addition, you will need to install the following dependencies if you do not already have them on your system.

  • Pdf2svg. This is required by TikzPictures. On Ubuntu, you can get this by running sudo apt-get install pdf2svg. On Windows, you can download the binaries from http://www.cityinthesky.co.uk/opensource/pdf2svg/. Be sure to add pdf2svg to your path (and restart).
  • Pgfplots (version 1.10 or later). Install using your latex package manager (e.g., texlive or miktex).
  • In order to plot contours, you will need GNUPlot installed and on your path.

Once these things are installed, you should be able to run the following:


In [2]:
using PGFPlots

You may receive some warnings. This is due to the Images.jl dependency. You can ignore these warnings.

Examples

Linear Plots

You can create a very basic plot by passing in vectors of $x$ and $y$ coordinates.


In [3]:
x = [1,2,3]
y = [2,4,1]
plot(x, y)


Out[3]:

The version of the plot function above actually just creates an empty Axis and inserts a Plots.Linear instance containing the data.


In [4]:
Axis(Plots.Linear(x, y))


Out[4]:

If you create the Axis object explicitly, as done above, then you can set various properties of the axis.


In [5]:
Axis(Plots.Linear(x, y), xlabel="X", ylabel="Y", title="My Title")


Out[5]:

Since latex is used to typeset everything, you can use any latex math symbols you want---just be sure that you do the necessary escaping.


In [6]:
Axis(Plots.Linear(x, y), xlabel="\$X\$", ylabel="\$Y\$", title="\$\\int_0^\\infty e^{\\pi x}dx\$")


Out[6]:

Histograms

It is very easy to make histograms. It is just another type under the Plots module. You should be able to use autocompletion in your editor (e.g., IJulia or LightTable) to see what Plots are supported.


In [7]:
d = randn(100)
Axis(Plots.Histogram(d, bins=10), ymin=0)


Out[7]:

You can even create a cumulative distribution function from the data.


In [8]:
Axis(Plots.Histogram(d, bins=20, cumulative=true, density=true), ymin=0)


Out[8]:

As with the other plots, you can control the style. The documentation on tikz and pgfplots can give you more information about what styles are supported.


In [9]:
Axis(Plots.Histogram(d, bins=10, style="red,fill=red!10"), ymin=0, ylabel="Counts")


Out[9]:

Image

Image plots create a PNG bitmap and can be used to visualize functions. The second and third arguments below are tuples specifying the x and y ranges.


In [10]:
f = (x,y)->x*exp(-x^2-y^2)
Plots.Image(f, (-2,2), (-2,2))


Out[10]:

Contour

Contours are produced using an external call to gnuplot, so make sure that it is installed properly. The syntax is similar to that of image.


In [11]:
Plots.Contour(f, (-2,2), (-2,2))


Out[11]:

You can specify the levels explicitly.


In [12]:
Plots.Contour(f, (-2,2), (-2,2), levels=[-0.4:0.1:4])


Out[12]:

You can also just specify the number of contours you want.


In [13]:
Plots.Contour(f, (-2,2), (-2,2), number=20)


Out[13]:

Group plots

A GroupPlot is just a container for plots. You can specify the number or columns and rows to use.


In [14]:
# generate 1000 samples
d = randn(1000)
# try out histograms with a variety of different number of bins
bins = [3 10 50 1000]
g = GroupPlot(2,2) # create a 2 x 2 group plot
for i = 1:length(bins)
    push!(g, Axis(Plots.Histogram(d, bins=bins[i]), ymin=0))
end
g


Out[14]:

Saving

All of the images above can be saved in tex, pdf, or svg format.


In [15]:
p = Plots.Histogram(rand(10))
save("myfile.tex", p)
save("myfile.pdf", p)
save("myfile.svg", p)

Future Plans

Gradually, more and more functionality from pgfplots will be migrated into this package. Eventually, this package will have scatter plots, constant plots, bar plots, comb plots, quiver plots, stacked plots, area plots, etc. It will also eventually expose more control over the axis configuration, error bars, tick marks, color data, etc.