5

I want to do a simple plot using Plots.jl.

I calculated a rate for each month over a couple of years. The problem that I am facing now is that I want to add a trendline to this plot. I did not find how this is done in Julia or Plots, if this is somewhere, please tell me.

My second question is that as I just get a vector with lets say 150 elements, each for a month, Plots.jl just gives me numbers on the x-axis for 0, 50, 100 and 150 with horizontal lines. I would like to change this to every 12 numbers one of these lines plus the year as a label on the axis.

I hope my question is clear, and thank you very much in advance.

Cheers

DoubleBass
  • 107
  • 1
  • 7

2 Answers2

5

No fancy features needed if I understand your question correctly.

using Plots
dates = 1:150
ticks = 1:12:150
ticks_labels = 0:12
values = rand(150).+dates*0.01
plot(dates, values, xticks = (ticks, ticks_label), label="my series")

bhat = [dates ones(150)]\values
Plots.abline!(bhat..., label = "trendline")

output -> enter image description here

pkofod
  • 764
  • 7
  • 18
  • Thank you pkofod, this was exactly what I was looking for. But another thing, do you know if it is possible not just to get a straight line for the trend but one the is smoother with the data as mine is quite volatile. – DoubleBass Apr 15 '17 at 09:27
  • Ok, I figured it out. I think what I wanted was an HP filter. Now it looks like I wanted it to look :) Thanks, again – DoubleBass Apr 15 '17 at 14:52
2

Plots now has a simple keyword option for adding a trend line.

using Plots
scatter(collect(1:10),collect(1:10)+rand(10),smooth=:true)
ahalwright
  • 71
  • 1
  • 4