naqvis / cryplot

Crystal plotting library powered by gnuplot
MIT License
22 stars 4 forks source link

Date / time data #4

Open hutou opened 1 month ago

hutou commented 1 month ago

Gnuplot documentation says:

gnuplot supports the use of time and/or date information as input data. This feature is activated by the commands set xdata time, set ydata time

How can I use these commands in Cryplot ? Thanks

naqvis commented 1 month ago

Please refer to Example code on using custom gnuplot commands

hutou commented 1 month ago

Sorry, but the example doesn't do what I want. I don't want to display integers (hours, days or years) in x, but dates, specifying the format (e.g. mm/yy). Is this possible? Thanks

naqvis commented 1 month ago

I don't want to display integers (hours, days or years) in x, but dates, specifying the format (e.g. mm/yy). Is this possible?

Yes. As long as functionality is supported by Gnuplot you can do that. For your required use-case, something like below would do the trick

ydata = [10, 15, 7, 20, 5]
dates = ["2024-10-01", "2024-10-05", "2024-10-10", "2024-10-15", "2024-10-20"]

plot = Cryplot.plot {
  size(480, 380)
  palette(:dark2)
  legend.hide
  xlabel("Date")
  ylabel("Value")
  title("Sample Plot with Dates on X-axis")

  gnuplot("set xdata time")
  gnuplot("set timefmt '%Y-%m-%d")
  xtics.rotate(-45)
  draw_boxes(dates, ydata)
  show
}

when executed above code, you will see something like (PS: I've rotated the x-axis labels to avoid cluttering. you can change that to any angle you like)

image

hutou commented 1 month ago

Ah, I hadn't seen the gnuplot function! Thanks