Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Plotting in Julia

Reese Pathak Stephen Boyd

EE103
Stanford University

September 22, 2017


Plotting options in Julia

I PyPlot (our recommendation)


– JuliaBox supports PyPlot
– PyPlot can be made to run locally (but it’s not straightforward)
I other options (not recommended): Plots, Gadfly

2
Fire up JuliaBox

I head to https://juliabox.com and login


I once you login, you will see a page that looks (somewhat) like

I select Julia 0.6.0, and enter the new notebook you’ve created

3
PyPlot in JuliaBox

I in the top cell of the notebook, run using PyPlot

I you should see no output from this line


I you’ve now successfully imported PyPlot

4
Plotting y = x2 for 0 ≤ x ≤ 10

# define range of x values


x = 0:0.1:10 # 0 to 10, step size 0.1
# define y
y = x.^2
# create a plot
plot(x,y)
# save image
savefig("ImageNameHere.png")

5
The result

6
Additional features in PyPlot

I figure legend, axis labels, title


– a label in the plot: plot(x, y, label="Label Goes Here")
– show labels: legend() (after plotting your lines)
– axis labels: xlabel("Label here"); ylabel("label")
– title: title("Title Goes Here")
I multiple lines: call plot(...) multiple times

7
A more detailed plot of y = x2

here’s a more detailed version of y = x2 from earlier (with code)

# collect plotting data


x = 1:0.1:10; y = x.^2
# plot line
plot(x,y, label=''y=x^2'')
# set x and y axis labels
xlabel(''x''); ylabel(''y'')
legend() # turn on legend
# add a title
title(''Plot of y = x^2'')
# save figure
savefig(''second_pyplot'')

8
A more complicated plot (1)

here’s a more complicated PyPlot example


# collect plotting data
x = 1:0.1:10
y1 = sqrt(x) + sin(log(x))
y2 = cos(x)
# plot line 1
plot(x,y1, label=''y=sqrt(x) + sin(log(x))'')
# plot line 2
plot(x,y2, label=''y=cos(x)'')
# set x and y axis labels
xlabel(''x''); ylabel(''y'')
legend() # turn on legend
# add a title
title(''Two plot figure'')
# save figure
savefig(''third_pyplot'')
9
A more complicated plot (2)
the resulting plot

10

You might also like