Gadfly.jl : How to plot date time based?

情到浓时终转凉″ 提交于 2021-02-18 11:38:25

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/22536691/gadfly-jl-how-to-plot-date-time-based

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!