Programming in JuliaPlotting
The main plotting package in Julia is called Plots
. To create a figure, you supply data in the form of arrays as arguments to the plot
function (x
first, then y
if appropriate, then z
if appropriate). All other plot information (called attributes, in Plots lingo) is supplied using keyword arguments. For example:
using Plots using Random Random.seed!(123) plot(rand(10), rand(10), seriestype = :scatter, group = rand(0:1,10), title = "Some random points")
Note that the group
keyword argument partitioned the data into two series, one for each unique value in the array supplied to group
. These series are automatically shown in different colors and labeled in the legend.
You can see all the main plot types and attributes on the Plots.jl cheatsheet.
To save a plot, use the savefig
function:
P = plot(rand(0:10),rand(0:10), seriestype=:scatter) savefig(P,"myfigure.pdf") # save figure as a PDF file
Exercise
Make a graph which looks as much as possible like the one shown below. You'll want to look at the Plots.jl cheatsheet for options.
using Plots x = range(0, stop = 2π, length = 100) y = sin.(x) # add plotting code here
Solution. We change the line style and width, and we add labels for the axes:
using Plots x = range(0, stop = 2π, length = 100) y = sin.(x) plot(x,y, linewidth = 3, linestyle = :dash, xlabel="x", ylabel="sin(x)", legend = :none)
To get a quick refresher on how to perform common tasks in Julia, check out the Julia-Python-R cheatsheet, also linked from browndsi.github.io.
Congratulations! You have finished the Data Gymnasia Programming with Julia course.