Plotting in Pluto
Itβs super easy to create plots in Pluto! There are many plotting libraries for Julia, and most will work well with Pluto.
Plots.jl
Plots.jl is an easy-to-use plotting library for Julia. It is a versatile plotting library, with a syntax that is similar to Pythonβs Matplotlib. To use it in Pluto, you add the following code to your notebook:
using Plots
plot([4,5,8,2,3,1,0])
Larger code blocks
You often want to combine multiple plot calls in a single cell. The let
block is really useful for this! Here is an example:
data = rand(100)::Vector{Float64}
let
plot(data)
# use the ! explamation mark to modify the previous plot
scatter!(data)
# add a horizontal line
mean = sum(data) / length(data)
hline!([mean]; label="Average value")
end
Using a let
block instead of begin
means that the variables defined in the block are local to the block, and they will not affect the rest of the notebook. In this example, the mean
variable is only defined inside the let
block, not in other cells.
My plot is not showing!
Sometimes, you create a plot in your code, but you donβt see it. This is because Pluto displays the last output of a cell . The plot
function βcreates a plotβ, but it only gets displayed if the cell output is that object.
For example, this code will not show a plot:
let
x = 1:10
y = rand(10)
plot(x, y, label="Data points")
for i in 1:length(x)
scatter!([x[i]], [y[i]], label="Point $i")
end
end
This is because the last expression in the let block is a for
loop, which returns nothing
. (So nothing
will be displayed.)
To fix it, add a plot!()
call at the end of the cell. This will get the current plot, and just return it.
let
x = 1:10
y = rand(10)
plot(x, y, label="Data points")
for i in 1:length(x)
scatter!([x[i]], [y[i]], label="Point $i")
end
# β
This is the fix!
plot!()
end
Take a look at the Plots.jl featured notebook to learn more!
Plots.jl
An introduction to Plots.jl
Makie.jl
Makie.jl is a powerful plotting library for Julia. It is built on top of the Julia plotting ecosystem and provides a simple interface for creating plots.
Not all Makie backends work well with Pluto. The best option is to use the GLMakie or CairoMakie backend. To see an example, take a look at this featured notebook:
3D Graph vs Heatmap
You can visualize 3D data with a 3D graph or a heatmap. How do they work, and what is the difference?
Convolutions
Learn about the cool concept of convolution on continuous functions!
Plotly
You can use the Plotly JS plotting library in Pluto! Check out the PlutoPlotly.jl package to get started.
More libraries
There are more (experimental) plotting libraries for Julia. They probably work with Pluto, try them out!