0

Suppose I have the following data and its plot:

using DataFrames
using StatsPlots
using Plots
using Dates

dates = Date(2021, 1, 1):Day(1):Date(2023, 3, 28)
n = length(dates)

df = DataFrame(
  date=dates,
  col1=rand(n),
  col2=rand(n)
)

@df df plot(
  :date, cols(2:3),
  legend=:outerright,
  xlabel="Date",
  ylabel="Val",
  title="Test plot",
  size=(1000, 600),
  dpi=300,
  left_margin=5Plots.mm,
  bottom_margin=5Plots.mm,
)

enter image description here

How can I change the precision of dates to monthly or quarterly in x-ticks?

Shayan
  • 5,165
  • 4
  • 16
  • 45

1 Answers1

3

You could use the xticks but you need to create a subset of DateTimes that you want to display in your x-axis, for example:

julia> tm_ticks = round.(dates, Month(1)) |> unique;
julia> plot(dates, rand(n), xticks=(tm_ticks, Dates.format.(tm_ticks, "uu/yyyy")), xrot=45, xminorticks=true, xlim=extrema(dates))

which will create something like: enter image description here

You can adjust tm_ticks, for instance, with Month(3) to create x-axis for quarterly ticks. ;)

pablosaa
  • 108
  • 1
  • 5