gpt4 book ai didi

datetime - 如何在 Julia 中创建高分辨率时间序列图

转载 作者:行者123 更新时间:2023-12-04 15:09:45 24 4
gpt4 key购买 nike

我已经使用 timeseries.jl 成功创建,版本 1.5.3 (2020-11-09),在 Juno 中,使用 JuliaPro 安装,代码如下

尝试 1:

using IterableTables
using DataFrames
using CSV
using Dates
using TimeSeries
using Plots


myfile="test2.csv"
dmft = dateformat"d/m/yyyy HH:MM:SS"
df = DataFrame(CSV.File(joinpath(@__DIR__,myfile); dateformat=dmft))
println(first(df,10))

ta = TimeArray(df; timestamp = :Date)
println(colnames(ta))
display(plot(ta[:Col3]))

并获得了这个图

timeseries plot in Juno在我的 REPL 中有以下输出

10×5 DataFrame
│ Row │ Date │ Col1 │ Col2 │ Col3 │ Col4 │
│ │ DateTime │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼─────────────────────┼─────────┼─────────┼─────────┼─────────┤
│ 1 │ 2020-08-10T00:00:00 │ 507.28 │ 181.34 │ 1532.96 │ 183.16 │
│ 2 │ 2020-08-10T00:01:00 │ 507.29 │ 181.34 │ 1532.95 │ 183.16 │
│ 3 │ 2020-08-10T00:02:00 │ 507.27 │ 181.34 │ 1532.94 │ 183.16 │
│ 4 │ 2020-08-10T00:03:00 │ 507.28 │ 181.34 │ 1532.97 │ 183.16 │
│ 5 │ 2020-08-10T00:04:00 │ 507.29 │ 181.33 │ 1532.97 │ 183.16 │
│ 6 │ 2020-08-10T00:05:00 │ 507.29 │ 181.33 │ 1532.96 │ 183.16 │
│ 7 │ 2020-08-10T00:06:00 │ 507.27 │ 181.33 │ 1532.95 │ 183.16 │
│ 8 │ 2020-08-10T00:07:00 │ 507.28 │ 181.33 │ 1532.96 │ 183.16 │
│ 9 │ 2020-08-10T00:08:00 │ 507.27 │ 181.33 │ 1532.95 │ 183.16 │
│ 10 │ 2020-08-10T00:09:00 │ 507.28 │ 181.32 │ 1532.96 │ 183.16 │
[:Col1, :Col2, :Col3, :Col4]

不幸的是,它以图像的形式出现,如果我缩放分辨率不高,如下所示。

zoomed in image

我希望实现的目标:

理想情况下,我更喜欢如下图所示的高分辨率图像,我可以使用 Shift 键和鼠标左键适本地放大它。

enter image description here

上图的数据框如下所示。

julia> print(first(mydf2,10))
10×8 DataFrame
│ Row │ ticker │ timestamp │ Open │ High │ Low │ Close │ AdjClose │ Volume │
│ │ String │ Date │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼────────┼────────────┼─────────┼─────────┼─────────┼─────────┼──────────┼───────────┤
│ 1 │ MSFT │ 2010-12-27 │ 28.12 │ 28.2 │ 27.88 │ 28.07 │ 22.3176 │ 2.16528e7 │
│ 2 │ MSFT │ 2010-12-28 │ 27.97 │ 28.17 │ 27.96 │ 28.01 │ 22.2699 │ 2.30422e7 │
│ 3 │ MSFT │ 2010-12-29 │ 27.94 │ 28.12 │ 27.88 │ 27.97 │ 22.2381 │ 1.95025e7 │
│ 4 │ MSFT │ 2010-12-30 │ 27.92 │ 28.0 │ 27.78 │ 27.85 │ 22.1427 │ 2.07861e7 │
│ 5 │ MSFT │ 2010-12-31 │ 27.8 │ 27.92 │ 27.63 │ 27.91 │ 22.1904 │ 2.4752e7 │
│ 6 │ MSFT │ 2011-01-03 │ 28.05 │ 28.18 │ 27.92 │ 27.98 │ 22.2461 │ 5.34438e7 │
│ 7 │ MSFT │ 2011-01-04 │ 27.94 │ 28.17 │ 27.85 │ 28.09 │ 22.3335 │ 5.44056e7 │
│ 8 │ MSFT │ 2011-01-05 │ 27.9 │ 28.01 │ 27.77 │ 28.0 │ 22.262 │ 5.89987e7 │
│ 9 │ MSFT │ 2011-01-06 │ 28.04 │ 28.85 │ 27.86 │ 28.82 │ 22.9139 │ 8.80263e7 │
│ 10 │ MSFT │ 2011-01-07 │ 28.64 │ 28.74 │ 28.25 │ 28.6 │ 22.739 │ 7.3762e7 │

使用来自 MarketData.jl 的数据和以下代码绘制:

using Gadfly
display(plot(mydf2,x="timestamp",y="AdjClose", Geom.line))

尝试 2:

我尝试用我的第一个数据系列来获得类似的结果,只是忽略了 TimeArray(因为它在尝试 1 中没有帮助),并得到了以下错误

myfile="test2.csv"
df = DataFrame(CSV.File(joinpath(@__DIR__,myfile)))
println(first(df,10))
display(plot(df,x="Date",y="Col3", Geom.line))

我收到以下数据框和错误消息:

    10×5 DataFrame
│ Row │ Date │ Col1 │ Col2 │ Col3 │ Col4 │
│ │ DateTime │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼─────────────────────┼─────────┼─────────┼─────────┼─────────┤
│ 1 │ 2020-08-10T00:00:00 │ 507.28 │ 181.34 │ 1532.96 │ 183.16 │
│ 2 │ 2020-08-10T00:01:00 │ 507.29 │ 181.34 │ 1532.95 │ 183.16 │
│ 3 │ 2020-08-10T00:02:00 │ 507.27 │ 181.34 │ 1532.94 │ 183.16 │
│ 4 │ 2020-08-10T00:03:00 │ 507.28 │ 181.34 │ 1532.97 │ 183.16 │
│ 5 │ 2020-08-10T00:04:00 │ 507.29 │ 181.33 │ 1532.97 │ 183.16 │
│ 6 │ 2020-08-10T00:05:00 │ 507.29 │ 181.33 │ 1532.96 │ 183.16 │
│ 7 │ 2020-08-10T00:06:00 │ 507.27 │ 181.33 │ 1532.95 │ 183.16 │
│ 8 │ 2020-08-10T00:07:00 │ 507.28 │ 181.33 │ 1532.96 │ 183.16 │
│ 9 │ 2020-08-10T00:08:00 │ 507.27 │ 181.33 │ 1532.95 │ 183.16 │
│ 10 │ 2020-08-10T00:09:00 │ 507.28 │ 181.32 │ 1532.96 │ 183.16 │
ERROR: LoadError: Cannot convert DataFrame to series data for plotting

尝试 3:

因为它是 DateTime 格式,我想知道为什么这是一个问题。好的,所以我现在尝试了一些不同的东西,在加载数据时不更改格式,并且仍然不使用 TimeArray:

myfile="test2.csv"
# dmft = dateformat"d/m/yyyy HH:MM:SS"
df = DataFrame(CSV.File(joinpath(@__DIR__,myfile))) # dateformat=dmft removed
println(first(df,10))

display(plot(df,x="Date",y="Col3", Geom.line))

但我还是得到了这个结果:

10×5 DataFrame
│ Row │ Date │ Col1 │ Col2 │ Col3 │ Col4 │
│ │ String │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼────────────────┼─────────┼─────────┼─────────┼─────────┤
│ 1 │ 10/8/2020 0:00 │ 507.28 │ 181.34 │ 1532.96 │ 183.16 │
│ 2 │ 10/8/2020 0:01 │ 507.29 │ 181.34 │ 1532.95 │ 183.16 │
│ 3 │ 10/8/2020 0:02 │ 507.27 │ 181.34 │ 1532.94 │ 183.16 │
│ 4 │ 10/8/2020 0:03 │ 507.28 │ 181.34 │ 1532.97 │ 183.16 │
│ 5 │ 10/8/2020 0:04 │ 507.29 │ 181.33 │ 1532.97 │ 183.16 │
│ 6 │ 10/8/2020 0:05 │ 507.29 │ 181.33 │ 1532.96 │ 183.16 │
│ 7 │ 10/8/2020 0:06 │ 507.27 │ 181.33 │ 1532.95 │ 183.16 │
│ 8 │ 10/8/2020 0:07 │ 507.28 │ 181.33 │ 1532.96 │ 183.16 │
│ 9 │ 10/8/2020 0:08 │ 507.27 │ 181.33 │ 1532.95 │ 183.16 │
│ 10 │ 10/8/2020 0:09 │ 507.28 │ 181.32 │ 1532.96 │ 183.16 │
ERROR: LoadError: Cannot convert DataFrame to series data for plotting

我怀疑问题出在日期或日期时间上,但我无法确定。有一篇关于绘制时间序列数据的帖子,但使用 String 代替。 Gadfly.jl : How to plot date time based?导致我在下面尝试:

尝试 4:

myfile="test2.csv"
dmft = dateformat"d/m/yyyy HH:MM:SS"
df = DataFrame(CSV.File(joinpath(@__DIR__,myfile); dateformat=dmft)) # historical data for the ticker

dt = Array(df.Date)
dt_str = Array(String,length(dt))
for i=1:length(dt)
dt_str[i] = string(dt[i]);
end

错误信息如下:

ERROR: LoadError: MethodError: no method matching Array(::Type{String}, ::Int64)

这是我的 csv 的一小段,如果您想尝试一下。

Date,Col1,Col2,Col3,Col4
10/8/2020 0:00,507.28,181.34,1532.96,183.16
10/8/2020 0:01,507.29,181.34,1532.95,183.16
10/8/2020 0:02,507.27,181.34,1532.94,183.16
10/8/2020 0:03,507.28,181.34,1532.97,183.16
10/8/2020 0:04,507.29,181.33,1532.97,183.16
10/8/2020 0:05,507.29,181.33,1532.96,183.16
10/8/2020 0:06,507.27,181.33,1532.95,183.16
10/8/2020 0:07,507.28,181.33,1532.96,183.16
10/8/2020 0:08,507.27,181.33,1532.95,183.16
10/8/2020 0:09,507.28,181.32,1532.96,183.16
10/8/2020 0:10,507.29,181.32,1532.97,183.16
10/8/2020 0:11,507.28,181.33,1532.94,183.16
10/8/2020 0:12,507.27,181.33,1532.96,183.16
10/8/2020 0:13,507.31,181.33,1532.96,183.17

我是 Julia 的新手,非常感谢任何初学者级别的指南。

编辑:这里的问题是情节被渲染为图像。我做了 svg,这就是我得到的。不是很吸引人吧?所有高分辨率数据都聚集在一起。

enter image description here

一旦它被渲染为图像,这就是 TimeSeries.jl 所做的,而不是使用 plotly 或 gladfly(或其他后端引擎)进行绘图,那么我就失去了放大绘图的能力。

只要是高分辨率,不渲染成图片,不管是plotly还是gladfly还是其他的我都可以。

是的,花盆很长。如果那没有帮助,请忽略我的代码。在帖子的最后,如果有人不介意向我展示应该如何正确完成,我提供了一个简短的 csv。又来了。

Date,Col1,Col2,Col3,Col4
10/8/2020 0:00,507.28,181.34,1532.96,183.16
10/8/2020 0:01,507.29,181.34,1532.95,183.16
10/8/2020 0:02,507.27,181.34,1532.94,183.16
10/8/2020 0:03,507.28,181.34,1532.97,183.16
10/8/2020 0:04,507.29,181.33,1532.97,183.16
10/8/2020 0:05,507.29,181.33,1532.96,183.16
10/8/2020 0:06,507.27,181.33,1532.95,183.16
10/8/2020 0:07,507.28,181.33,1532.96,183.16
10/8/2020 0:08,507.27,181.33,1532.95,183.16
10/8/2020 0:09,507.28,181.32,1532.96,183.16
10/8/2020 0:10,507.29,181.32,1532.97,183.16
10/8/2020 0:11,507.28,181.33,1532.94,183.16
10/8/2020 0:12,507.27,181.33,1532.96,183.16
10/8/2020 0:13,507.31,181.33,1532.96,183.17

最佳答案

这个好像有用

显然,如果它在 String 中,它仍然有效。不知道为什么我昨天没有尝试这个

myfile="test2.csv"
dmft = dateformat"d/m/yyyy HH:MM:SS"
df = DataFrame(CSV.File(joinpath(@__DIR__,myfile)))
println(first(df,10))
using Gadfly
display(plot(df2, x="Date", y="Col3", Guide.xticks(label=false), Geom.line, Theme(grid_line_width=0mm)))

我试过 plotly,效果更好。我在一篇文章中说 DateTime 必须在字符串中,这让我陷入了困境。那不是真的。

using IterableTables
using DataFrames
using CSV
using Dates
using Plots
myfile="test2.csv"
dmft = dateformat"d/m/yyyy HH:MM:SS"
df = DataFrame(CSV.File(joinpath(@__DIR__,myfile); dateformat=dmft))
println(first(df,10))
df2 = filter(row -> row[:Date] <= Dates.DateTime("2020-10-15T00:06:00"), df)
plotly()
using StatsPlots
@df df plot(:Date, :Col3)

Plotly in Juno

关于datetime - 如何在 Julia 中创建高分辨率时间序列图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65432468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com