Warm tip: This article is reproduced from serverfault.com, please click

其他-如何在Julia中使用Time数据获得更好的Plots.jl xaxis刻度标签?

(其他 - How to get better Plots.jl xaxis tick labels with Time data in Julia?)

发布于 2020-12-08 19:57:08

我希望看到[00:00:00, 02:00:00, 04:00:00, 06:00:00]类似xtick的标签。pyplot和GR后端给出了相似的结果。我可以使用支持左和右yaxis的任何后端。

默认图

using DataFrames, Dates, Plots
gr(linewidth=3, markersize=4, titlefont=18, legendfont=12, guidefont=18, tickfont=14, legend=:outertopright)
df = DataFrame()
df.Time = ["0:05:02", "1:05:23", "2:05:34", "4:05:42", "5:36:01"]
df.Data = [0.0, 1.0, 0.5, 0.4, 0.1]
df.Time = Time.(df.Time, DateFormat("H:M:S"))
plot(df.Time, df.Data)

default.png

我的尝试

julia> plot(df.Time, df.Data, lims = :round)

lims.png

julia> plot(df.Time, df.Data, ticks = 4)

ticks.png

julia> plot(df.Time, df.Data, lims = :round, ticks = 4)

lims_ticks.png

julia> plot(df.Time, df.Data, xlims = (Time(0), Time(6)))
ERROR: MethodError: no method matching Float64(::Time)
Closest candidates are:
  Float64(::UInt32) at float.jl:66
  Float64(::Int128) at float.jl:94
  Float64(::Float16) at float.jl:256
  ...
Stacktrace:
 [1] (::RecipesPipeline.var"#7#8"{Symbol})(::Time) at C:\Users\nboyer.AIP\.julia\packages\RecipesPipeline\uPBKQ\src\utils.jl:201
 [2] optimal_ticks_and_labels(::Plots.Subplot{Plots.GRBackend}, ::Plots.Axis, ::Nothing) at C:\Users\nboyer.AIP\.julia\packages\Plots\vsE7b\src\axes.jl:167
julia> plot(df.Time, df.Data, xticks = Time(0):Hour(2):Time(6))

xticks.png

上次尝试设置xticks已关闭,但我仍然无法显示最后的勾号。有趣的是,我将范围的最后一个值设置为什么都没有关系。xticks = Time(0):Hour(2):Time(10)产生相同的图像。

Questioner
Nathan Boyer
Viewed
11
MarcMush 2020-12-10 19:27:18

这将起作用:

plot(df.Time, df.Data, 
     xlims = (Dates.value(Time(0)), Dates.value(Time(6))), 
     xticks = Time(0):Hour(2):Time(6)
)

我觉得没有这应该是可能的Dates.value

最近,加入了支持DateDateTimehttps://github.com/JuliaPlots/Plots.jl/pull/3180),但没有TimeTimePeriod