The library we will focus on here
using Gadfly
f = [ abs(1/x^2 - randn()/x^2.5) for x in 1:1000 ]
p = plot(y=f, Geom.line, Scale.y_log10,
Guide.xlabel("Iteration"), Guide.ylabel("Function Value"))
using PyPlot
# pygui(true) # interactive figures in new window
f = [ abs(1/x^2 - randn()/x^2.5) for x in 1:1000 ]
xlabel("Iteration")
ylabel("Function Value")
semilogy(f) # log10 scale
plot(f)
1-element Array{Any,1}: PyObject <matplotlib.lines.Line2D object at 0x8bdde86c>
using Winston # produces warnings - outdated?
f = [ abs(1/x^2 - randn()/x^2.5) for x in 1:1000 ] # random values
semilogy(f)
WARNING: deprecated syntax "AbstractVecOrMat{T} (" at /home/mbunse/.julia/v0.4/Winston/src/Winston.jl:96. Use "AbstractVecOrMat{T}(" instead. WARNING: requiring "Dates" in module "Winston" did not define a corresponding module. WARNING: module Winston should explicitly import * from Base
using Gadfly
# 1) x and y arrays
x=rand(10)
y=rand(10)
plot(x=x, y=y) # x and y are named arguments
# plot(y=y) # x can be omitted
# 2) DataFrames
using DataFrames
df = DataFrame(
dfX = x,
dfY = y
)
plot(df, x=:dfX, y=:dfY) # x="dfX", y="dfY" also possible
using RDatasets # example DataFrames
plot(dataset("datasets", "iris"), x=:SepalLength, y=:SepalWidth)
# 3) Functions
plot(sin, 0, 4π)
# plot(x=x, y=y) # default: scatter plot (Geom.point)
plot(x=x, y=y, Geom.line)
# plot(x=x, y=y, Geom.point, Geom.line) # combine geometries
# plot(x=x, y=y, Geom.point, Geom.smooth) # fit smooth curve in
# plot(x=x, y=y, Geom.line, Stat.step) # step function
plot(x=randn(1000), Geom.histogram)
# plot(x=randn(1000), Geom.histogram(bincount=20))
# more params: density-histogram, orientation, stacking,...
Other geometries:
plot(sin, 0, 4π, Guide.xticks(ticks=[0, π, 2π, 3π, 4π]))
# plot(sin, 0, 4π, Guide.xticks(ticks=[0, π, 2π, 3π, 4π]),
# Scale.x_continuous(labels=x -> @sprintf("%dπ", x/π))) # scale labels
# example from introduction:
f = [ abs(1/x^2 - randn()/x^2.5) for x in 1:1000 ]
plot(y=f, Geom.line, Scale.y_log10,
Guide.xlabel("Iteration"), Guide.ylabel("Function Value"),
Guide.title("Awesome Optimization"))
plot(x=x, y=y, Geom.line, Geom.point, Theme(
default_color=colorant"red",
line_width=5pt,
default_point_size=10pt,
major_label_font_size=24pt
)) # There are many more parameters!
f_1 = [ abs(1/x^2 - randn()/x^2.5) for x in 1:1000 ]
f_2 = [ abs(1/x^3 - randn()/x^3.6) for x in 1:1000 ]
plot(
# two layers with different colors
layer(y=f_1, Geom.line, Theme(default_color=colorant"red")),
layer(y=f_2, Geom.line, Theme(default_color=colorant"blue")),
# log scale and axis labels
Scale.y_log10,
Guide.xlabel("Iteration"), Guide.ylabel("Function Value"),
# legend
Guide.manual_color_key("Legend",
["Solver A", "Solver B"],
[colorant"red", colorant"blue"])
)
p_1 = plot(sin, 0, 4π)
p_2 = plot(x -> sin(x)^8, 0, 4π)
vstack(p_1, p_2) # vertical stacking
# hstack(p_1, p_2) # horizontal stacking
# hstack(vstack(p_1, p_2), vstack(p_2, p_1)) # nested stacking
using Cairo # provides pdf and png backends
draw(PDF("p_2.pdf", 12cm, 6cm), p_2)
# draw(PNG("p_2.png", 12cm, 6cm), p_2)
# IJulia integration: Default plot size
set_default_plot_size(20cm, 4cm)
p_1 # will be displayed wide and flat