Tutorials > Julia Programming: a Hands-on Tutorial
Some Visualization Libraries to Choose From
A quick reference on the many options to visualize data with Julia.
There are multiple plotting packages for Julia worth checking:
|Plots.jl||provides a single API to access multiple “backends”, which inlclude Matplotlib (Pyplot), Plotly, and GR.||Pyplot, Plotly, GR.||Docs|
|StatsPlots.jl||A drop-in replacement for Plots.jl that contains specialized statistical plotting functionalities.||StatsPlots.jl repository||Plots.jl docs|
|Makie.jl||A high-performance plotting ecosystem with OpenGL, Cairo and WebGL backends. It’s great for publication-quality plotting, but can be a little bit slow to load and use||Docs|
|VegaLite||A Julia wrapper for the Vega-Lite library. Great for interactive graphics.||Docs.|
|Gadfly||Based on the R package gglot2, very well suited for statistics and machine learning.||Docs|
Detailed documentation can be found in each package, and in the referenced tutorials and examples pages.
To keep this tutorial series as much as self-contained as reasonably possible, let’s go over a few examples here:
Plotting a Function with Plots.jl
using Plots # 10 points of random data, in two columns x = 1:10; y = rand(10, 2); plot(x, y, title = "Two Lines", label = ["Line 1" "Line 2"], marker = ([:hex :d], 8), lw = 3) xlabel!("My x label")
Displaying a Pseudocolor Plot of a 2D Array
One way of doing a 2d pseudocolor plot with Julia is to use the ‘heatmap’ function.
using Plots # Generate some 2D data x = LinRange(-1,1,100); Z = zeros(100,100) for i=1:100, j=1:100 r = x[i]^2 + x[j]^2 Z[i,j] = sin(10*r) / (1+r) end heatmap(Z)
Calling Matplotlib’s PyPlot with PyPlot.jl
Alternatively, we could also use the PyPlot package, which provides a direct interface to Matplotlib’s Pyplot via PyCall, namely to the matplotlib.pyplot module.
For example, the above example can be modified as follows:
using PyPlot # Replace Plots with PyPlot pcolormesh(Z) # Replace heatmap with pcolormesh
to produce the following plot:
Note that using both PyPlot and Plots could result in errors, so native Julia libraries should be preferred.
Don't miss any updates!
I'm writing a newsletter about once a month, with links to new posts and tutorials.
In particular, one of my goals for 2022 is to write a book about scientific software development using Julia. Make sure to subscribe if you are interested!