Gadfly.jl:如何基于日期时间绘制? [英] Gadfly.jl : How to plot date time based?
问题描述
我正在尝试根据时间绘制数据:
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})
我发现的唯一解决方案是创建一个字符串数组,然后用新的字符串数组初始化 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屋!