Gadfly.jl:如何根据日期时间绘制?

时间:2014-03-20 14:51:47

标签: julia

我正在尝试根据时间绘制数据:

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)

我有这个错误:

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

任何提示?

1 个答案:

答案 0 :(得分:3)

问题在于将函数调用draw(...)中的Datetime值转换为DataFrame中的字符串值。

请参阅以下代码

我已将变量dt分配给日期时间值

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

如果您甚至尝试将其转换为字符串,则会出现以下错误

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

因此错误

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})

我发现的唯一解决方案是创建一个String数组,然后使用新的String数组初始化DataFrame

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

现在因为我们在DataFrame中只有字符串代替Datetime,所以不会出现任何错误

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