Reputation: 1796
I have the following code:
using Plots
using TimeSeries
apple_ta = readtimearray("apple.csv")
p = plot(apple_ta[:Open])
Plots.xticks(p)
1-element Vector{Tuple{Vector{Float64}, Vector{String}}}:
([735599.0, 736330.0, 737060.0, 737791.0], ["2015-01-01", "2017-01-01", "2019-01-01", "2021-01-01"])
When I print the xticks, I get the above output but have no idea where values like 735599 are from. First, I thought they must be timestamps of the dates from the XAxis but they aren't. Where do they come from?
My final goal is to be able to set the xticks with xticks!
function to whichever dates I want. Does anybody know how to?
PS. Here is the top 5 lines of apple.csv:
Date,Open,Close,Volume
2015-01-02,27.84749984741211,27.332500457763672,212818400
2015-01-05,27.072500228881836,26.5625,257142000
2015-01-06,26.635000228881836,26.565000534057617,263188400
2015-01-07,26.799999237060547,26.9375,160423600
2015-01-08,27.3075008392334,27.97249984741211,237458000
Upvotes: 0
Views: 323
Reputation: 13800
To answer your question directly, Dates.value(::Date)
returns the number of days since December 31st, year 0:
julia> Dates.value(Date(0, 12, 31))
0
julia> Dates.value(Date(1, 1, 1))
1
However it seems to me that something is off with your TimeArray
- there's a plot recipe in TimeSeries
for plotting data with correctly formatted xticks, here's an MWE using the data you provided:
julia> using DataFrames, TimeSeries, Plots, DelimitedFiles
julia> df = readdlm(IOBuffer("""Date,Open,Close,Volume
2015-01-02,27.84749984741211,27.332500457763672,212818400
2015-01-05,27.072500228881836,26.5625,257142000
2015-01-06,26.635000228881836,26.565000534057617,263188400
2015-01-07,26.799999237060547,26.9375,160423600
2015-01-08,27.3075008392334,27.97249984741211,237458000
"""), ',')
6×4 Matrix{Any}:
"Date" "Open" "Close" "Volume"
"2015-01-02" 27.8475 27.3325 212818400
"2015-01-05" 27.0725 26.5625 257142000
"2015-01-06" 26.635 26.565 263188400
"2015-01-07" 26.8 26.9375 160423600
"2015-01-08" 27.3075 27.9725 237458000
julia> ts = TimeArray(DataFrame(date = Date.(df[2:end, 1]), value = df[2:end, 3]); timestamp = :date)
5×1 TimeArray{Any, 1, Date, Vector{Any}} 2015-01-02 to 2015-01-08
│ │ value │
├────────────┼─────────┤
│ 2015-01-02 │ 27.3325 │
│ 2015-01-05 │ 26.5625 │
│ 2015-01-06 │ 26.565 │
│ 2015-01-07 │ 26.9375 │
│ 2015-01-08 │ 27.9725 │
julia> plot(ts)
This produces:
Slightly less than ideal cropping on the right hand side, but this can be fixed by either adding right_margin = 5Plots.mm
or rotating the xticks via xrot = 30
(or some other value).
Are you maybe on some outdated version of TimeSeries
or Plots
? Here are the versions used to produce the above:
(jl_ypFE6F) pkg> st
Status `/tmp/jl_ypFE6F/Project.toml`
[a93c6f00] DataFrames v1.4.4
[91a5bcdd] Plots v1.38.0
[9e3dc215] TimeSeries v0.23.1
Upvotes: 0
Reputation: 1796
The numbers returned by xticks for each date are the result of calling Dates.value
function:
using Dates
dt = Date(2015, 01, 01)
value = Dates.value(dt)
>>> println(value)
735599
So, if you want to set custom xticks when plotting dates, you can do the following:
dates = [Date(2015, 01, 01), Date(2016, 01, 01), Date(2017, 01, 01)]
ticks = Dates.value.(dates)
labels = string.(dates)
xticks!(ticks, labels, rotation=20)
Upvotes: 1