Plotting is one of the bigger differences from Matlab, although, there are still many similarities.
The first thing we need to note is that plotting is not in Julia by default. You need to add plotting packages in order to get it.
Pkg.add("Plots")
Pkg.add("PyPlot")
These commands show "Nothing to be done" because I already have the packages! But if you don't, they will get them. You only need to do this once for your Julia, so I never have to run these commands again! Yay! Once I've run then, then I can make plots.
using Plots
This command loads the plotting library. So now we can make plots.
x = 0:0.1:1
y = cos(50*x)
plotly()# this says to use the Plotly backend, which has nice interactive features
# but sends data over the internet :(
plot(x,y)
pyplot() # This says to use the Pyplot backend, which keeps all data locally,
# but doesn't have the interactive features.
plot(x,y)
You don't have to execute plotly()
or pyplot()
before each command, it'll remember.
x = 0:0.01:1
y = cos(50*x)
plot(x,y)
title!("Plot of x versus cos(50x)")
ylabel!("cos(50x)")
xlabel!("x")
plot(x,cos(50*x),label="cos(50x)")
plot!(x,x,label="x") # add a second line
# A plot with two lines and a legend (Figure 2.3)
If you want to include plots into your homework in Latex, you can save the plots.
savefig("myplot.pdf") # this saves a PDF file
savefig("myplot.png") # this saves a PNG file