This file demonstrates how to create plots in Julia by using the PyPlot package.
PyPlot relies on the matplotlib library, which is part of Python. If you have Python installed, then it will be used as is. Otherwise, see PyPlot's homepage for instructions on how to install.
Collections of examples are available at Plot Examples and the Julia Plots Gallery.
In [1]:
using Dates
using PyPlot
PyPlot.svg(true) #prettier figures
Out[1]:
In [2]:
x = range(-3,stop=3,length=20)
y = 2*x.^2 .+ 0.5
figure(figsize=(6,4.3)) #width and height, in inches
plot(x,y,linestyle="-",color="r",linewidth=1.0)
plot(x,log.(y),linestyle=":",color="b",linewidth=3.0)
#display(gcf()) #uncomment if the plot is not shown (eg. in REPL or VS Code)
Out[2]:
In [3]:
#now with title, labels and more - and larger
figure(figsize=(8,5.71))
plot(x,y,linestyle="-",color="r",linewidth=1.0,label="y")
plot(x,log.(y),linestyle=":",color="b",linewidth=3.0,label="log y")
xticks([-2;0;2],["A";"B";"C"])
yticks([0;5;6;10])
grid(true)
title("a title",fontsize=20)
xlim(-3,3) # set limits of the x-axis
ylim(-1,20) # set limits of the y-axis
xlabel("x")
ylabel("function values")
text(-2.5,0.9,"some text")
legend(loc="upper center")
savefig("AFirstPlot.pdf") #save pdf file of the plot
#display(gcf())
In [4]:
fig = figure(figsize=(8,5.7)) #subplots
subplot(2,2,1)
plot(x,y,"r-")
title("a. first")
xlabel("x")
subplot(2,2,2)
plot(x,log.(y),"b--")
title("b. second")
xlabel("x")
subplot(2,2,3)
plot(x,log.(y).^2,"k:")
title("c. third")
xlabel("x")
subplots_adjust(hspace = 0.4) #to give more vertical space between "row" 1 and 2
#fig.set_size_inches(16,10.7,forward=false) #to save really large figure
#savefig("ASecondPlot.pdf")
#display(gcf())
In [5]:
figure(figsize=(6,4.3))
plot(x,y)
xlim(-3,3)
ylim(0,20)
title("With reference lines")
xlabel("x")
ylabel("function value")
hlines(11,-2.5,2.5,linestyle="--",color="black") #stretches over x=[-2.5;2.5]
axhline(11.5,linestyle="-.",color="g") #stretches over all x
vlines(0.5,0,20,linestyle=":",color="r")
#display(gcf())
Out[5]:
In [6]:
PyPlot.matplotlib.rc("mathtext",fontset="cm") #computer modern font
PyPlot.matplotlib.rc("font",family="serif",size=12) #font similar to LaTeX
figure(figsize=(6,4.3))
plot(x,y)
title(L"a title using LaTeX, $2 b^2 + 0.5$")
xlabel(L"$\alpha$")
ylabel("function value")
text(-2.5,0.9,L"some text, $\ln(\mathrm{loss})$")
text(-2.5,5,L"$\mu_2 = \int x^2 f(x) dx$")
#display(gcf())
PyPlot.matplotlib.rc("font",family="sans-serif",size=12) #resetting the font
In [7]:
figure(figsize=(6,4.3))
bar(x,y,facecolor="red",edgecolor="white",align="center",width=0.3)
xlim(-2.5,2.5)
ylim(-1,15)
title("Bar chart")
txt = "something important"
annotate(txt,xy=[0.7;0.25],xycoords="axes fraction",xytext=[0.5;0.5],
textcoords="axes fraction",arrowprops=Dict("facecolor"=>"black","width"=>0.5))
#display(gcf())
Out[7]:
In [8]:
figure(figsize=(6,4.3))
step(x,y,linewidth=1.5,color="r")
xlim(-2.5,2.5)
ylim(-1,15)
title("Stairs plot")
#display(gcf())
Out[8]:
In [9]:
x = range(-3,stop=3,length=20) #create some "data" to plot
y = range(1,stop=7,length=25)
z = fill(NaN,(length(x),length(y))) #to put results in, initialized as NaNs
for j = 1:length(y) #create z column by column
z[:,j] = 2*x.^2 .+ (y[j]-4)^2 - 0.0*x.*(y[j]-4)
end
#notice the arguments: x,y,z'
println(size(x),size(y),size(z'))
PyPlot.matplotlib.rc("font",size=16)
fig = figure(figsize=(12,8))
surf(x,y,z',rstride=1,cstride=1,cmap=ColorMap("cool"),alpha=0.8)
xlim(-3,3) #change rstride and cstride to improve the look
ylim(1,7)
zlim(0,30)
xlabel("x")
ylabel("y")
title("Surface plot")
#display(gcf())
Out[9]:
In [10]:
using3D() #for projection="3d" below
PyPlot.matplotlib.rc("font",size=16)
figure(figsize=(12,8))
ax = gca(projection="3d") #notice
surf(x,y,z',rstride=1,cstride=1,cmap=ColorMap("cool"),alpha=0.8)
xlim(-3,3)
ylim(1,7)
zlim(0,30)
ax.view_init(elev=20.0,azim=10) #notice: elevation and azimuth
xticks(-3:2:3)
yticks(2:2:6)
ax.set_zlim([0,25])
ax.set_zticks([0;10;20])
xlabel("x")
ylabel("y")
title("Surface plot, rotated")
#display(gcf())
Out[10]:
In [11]:
PyPlot.matplotlib.rc("font",size=14)
fig1 = figure(figsize=(6,4.3))
ax = gca()
lev = [0.25;0.5;1:1:10]
contour(x,y,z',lev,cmap=ColorMap("cool"))
xlim(-3,3)
ylim(1,7)
xlabel("x")
ylabel("y")
title("Contour plot")
#display(gcf())
Out[11]:
In [12]:
N = 51
x = randn(N) #SCATTER, HISTOGRAM
y = rand(N)
areas = rand(51)*100 #size of the scatter points, could be a scalar
figure(figsize=(6,4.3))
scatter(x,y,s=areas,alpha=0.5) #s is the size of the circles
title("Scatter plot")
xlabel("x")
ylabel("y")
#display(gcf())
Out[12]:
In [13]:
figure(figsize=(6,4.3))
Bins = -2.5:0.25:2.5
hist(x,bins=Bins,density=true,color="yellow",edgecolor="k")
grid(true)
title("Histogram (with area = 1)")
xlabel("x")
#display(gcf())
Out[13]:
In [14]:
dN = Date(2013,12,4):Dates.Day(1):Date(2016,12,31) #just faking some dates
y = randn(length(dN)) #some random numbers to plot
xTicks = Date(2014,1,1):Dates.Year(1):Date(2017,1,1) #tick marks on x axis
figure(figsize=(7,7/1.4)) #basic time series plot
plot_date(dN,cumsum(y),"k-")
xticks(xTicks)
title("A random walk",fontsize=18)
#display(gcf())
Out[14]:
In [15]:
figure(figsize=(7,7/1.4))
plot_date(dN,cumsum(y),"k-")
xticks(xTicks)
gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y"))
title("A random walk, with better tick marks",fontsize=18)
#display(gcf())
Out[15]:
In [ ]: