Plotting using Plots.jl¶

(Back to Overview)

In [1]:
using Plots
In [3]:
x  = range(-10, 10, step=0.1)
y1 = x.^2
y2 = x.^3;
In [4]:
plot(x, y1)
Out[4]:
In [5]:
plot!(x, y2)
Out[5]:
In [6]:
data = x .+ 2 .* (rand(length(x)) .- 0.5)
Out[6]:
201-element Vector{Float64}:
 -10.31719114891199
  -9.252777808251585
  -9.727274506201773
 -10.533496184224852
 -10.124384166706065
 -10.469282901901282
  -9.34864114762013
  -8.38139904171323
  -9.692686210106158
  -8.751845722515894
  -8.322203422262488
  -8.32868832229196
  -8.908576450328301
   ⋮
   8.235039042035996
   9.899622717871985
   9.967513447235895
   9.300076417934994
   9.689476207326404
  10.120459761277047
   9.559907388490089
   8.957913889734394
   9.772912793884611
   8.972580732459075
  10.825929492745397
   9.945407457616637
In [7]:
plot(x, data, line=nothing, marker=:circ, legend=:topleft, label="noise")
plot!(x, x, line=(:solid, 2), label="original")
Out[7]:
In [13]:
using LsqFit
In [14]:
model(x, p) = @. p[1] + p[2]*x
Out[14]:
model (generic function with 1 method)
In [15]:
fit = curve_fit(model, x, data, [0., 0.])
fit.param
Out[15]:
2-element Vector{Float64}:
 -0.01447258312225462
  1.0053215561142779
In [16]:
plot!(x, model(x, fit.param), line=(:solid, 2), label="fit")
Out[16]:
In [ ]: