# How to plot a best fit curve in Julia when the data is a time series?

**URL:** https://discourse.julialang.org/t/how-to-plot-a-best-fit-curve-in-julia-when-the-data-is-a-time-series/79317
**Category:** Visualization
**Tags:** plotting, time-series, curve-fitting
**Created:** [April 10, 2022, 9:37pm UTC](https://discourse.julialang.org/t/how-to-plot-a-best-fit-curve-in-julia-when-the-data-is-a-time-series/79317 "2022-04-10T21:37:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![wu-lee](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@wu-lee](https://discourse.julialang.org/u/wu-lee)
#### Post date: [April 10, 2022, 9:37pm UTC](https://discourse.julialang.org/t/how-to-plot-a-best-fit-curve-in-julia-when-the-data-is-a-time-series/79317/1 "2022-04-10T21:37:36Z")

</div>

This is a copy of my question here on [Stack Overflow](https://stackoverflow.com/questions/71726292) (unanswered at the time of writing).

I’m trying to plot some measurements over time with a best-fit curve, using Julia’s plotting tools. There’s already a fairly good answer how to do this with a simple numeric data series:

[Julia: How to find best fit curve / equation when I have a plot?](https://stackoverflow.com/questions/65895160/julia-how-to-find-best-fit-curve-equation-when-i-have-a-plot)

However, these tools (well - at least the Polynomials package) do not like being given Date values. I imagine some sort of conversion from Dates into a numeric value needs to be done, but I want the resulting plots to retain their scales in date units, which would need a conversion back again.

I suspect this is a fairly common problem and there must be an elegant way to solve it, but being new to Julia I would like to ask for some pointers to what a good solution looks like?

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [April 11, 2022, 5:48am UTC](https://discourse.julialang.org/t/how-to-plot-a-best-fit-curve-in-julia-when-the-data-is-a-time-series/79317/2 "2022-04-11T05:48:25Z")

</div>

I don’t think there is a package doing what you want. If you don’t mind the conversion, you need to do it only once, to do the fit; you plot against the original date values:

```julia
using LsqFit: curve_fit
using Plots

Date2Val(x::DateTime) = x.instant.periods.value # DateTime to value converter
m(t, p) = p[1] * exp.(p[2] * t) # the model
dt = sort(DateTime.(2022, 4, 11, 1, 30, rand(1:30, 15)),
          rev=true) # generate random DateTime values and sort them from earlier to later
x = Date2Val.(dt) .- Date2Val(dt[end]) # convert DateTime to a numerical value
y = m(x, [1, -1e-4]) + 1e-2randn(length(dt)) # generate the y values and add some noise
fit = curve_fit(m, x, y, [1.4, -1e-2]) # do the least square fit using LsqFit
scatter(dt, y, label="data") # plot data ...
plot!(dt, m(x, fit.param), label="fit") # ... and fit both agains dates

```

![image](https://global.discourse-cdn.com/julialang/original/3X/f/a/fa80df3ccedbeeeef1a73e44aa92a73d3f4320a4.png)

---

<div class="post-metadata">

### Author: ![wu-lee](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@wu-lee](https://discourse.julialang.org/u/wu-lee)
#### Post date: [April 15, 2022, 4:57pm UTC](https://discourse.julialang.org/t/how-to-plot-a-best-fit-curve-in-julia-when-the-data-is-a-time-series/79317/3 "2022-04-15T16:57:54Z")

</div>

Thanks! I think that does the job nicely.
