Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gadfly.jl : How to plot date time based?

Tags:

julia

I'm trying to plot data based on time :

userId = 2
dateGiven = Datetime.date(2014,2,3)
biometric = "heart-rate"
df = DataFrames.readtable(string("data/user_",userId,"/",dateGiven,"/",biometric,".csv"),
                      colnames = ["Time", "Heart Rate"],
                      coltypes = {Int,Int}  )

df["Time"] = map((timestamp) -> Datetime.unix2datetime(timestamp, Datetime.UTC) , df["Time"])
plot(df,x="Time",y="Heart Rate",Geom.line)

I have this error :

no method *(Float64,DateTime{ISOCalendar,Zone0})
 in optimize_ticks at /Users/nhenin/.julia/v0.2/Gadfly/src/ticks.jl:77
 in apply_statistic at /Users/nhenin/.julia/v0.2/Gadfly/src/statistics.jl:584
 in apply_statistics at /Users/nhenin/.julia/v0.2/Gadfly/src/statistics.jl:35
 in render at /Users/nhenin/.julia/v0.2/Gadfly/src/Gadfly.jl:636
 in writemime at /Users/nhenin/.julia/v0.2/Gadfly/src/Gadfly.jl:738
 in sprint at io.jl:434
 in display_dict at /Users/nhenin/.julia/v0.2/IJulia/src/execute_request.jl:35

Any hints ?

like image 873
Nicolas Henin Avatar asked Mar 20 '14 14:03

Nicolas Henin


1 Answers

The problem is with conversion of the Datetime values to string values from DataFrame in the function call draw(...).

See the below code

I have assigned a variable dt to the datetime values

julia>dt
100-element Array{DateTime{ISOCalendar,Zone0},1}:
2014-03-21T11:48:10 UTC
2014-03-21T11:48:11 UTC
2014-03-21T11:48:12 UTC
.
.
.
2014-03-21T11:49:47 UTC
2014-03-21T11:49:48 UTC
2014-03-21T11:49:49 UTC

If you even try to convert this into string it will give the following error

julia> dt[1] = string(dt[1])
no method convert(Type{DateTime{ISOCalendar,Zone0}},ASCIIString)

Hence the error

julia> p = plot(Df,x="Time",y="Heart_Rate",Geom.line)
Plot(...)

julia> draw(PNG("HeartRate.png",5inch,5inch),p)
no method *(Float64,DateTime{ISOCalendar,Zone0})

The only Solution I found out is to create a String array and then initialize the DataFrame with the new String array

julia> dt_str = Array(Any,length(dt))
julia> for i=1:length(dt)
dt_str[i] = string(dt[i]);
end

julia> Df = DataFrame(Time = dt_str,Heart_Rate = heart_rate)
100x2 DataFrame:
                               Time Heart_Rate
[1,]      "2014-03-21T11:48:10 UTC"    76.2317
[2,]      "2014-03-21T11:48:11 UTC"    80.0101
[3,]      "2014-03-21T11:48:12 UTC"    66.6338
.
.
.
[98,]     "2014-03-21T11:49:47 UTC"    96.9248
[99,]     "2014-03-21T11:49:48 UTC"    94.6423
[100,]    "2014-03-21T11:49:49 UTC"    92.9679

Now as we have only strings in place of Datetime in the DataFrame it wont give any sort of error

julia> p = plot(df,x="Time",y="Heart_Rate",Geom.line)
Plot(...)

julia> draw(PNG("HeartRate.png",5inch,5inch),p)


julia> 

enter image description here

like image 56
Jay Dharmendra Solanki Avatar answered Oct 18 '22 22:10

Jay Dharmendra Solanki