Gadfly.jl:如何绘制基于日期的时间? [英] Gadfly.jl : How to plot date time based?

查看:67
本文介绍了Gadfly.jl:如何绘制基于日期的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我遇到此错误:

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

有什么提示吗?

推荐答案

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

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

请参见下面的代码

我已为日期时间值分配了变量dt

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)

出现错误

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

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

现在,由于我们在DataFrame中仅使用字符串代替Datetime,因此不会出现任何错误

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> 

这篇关于Gadfly.jl:如何绘制基于日期的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆