KristofferC / PGFPlotsX.jl

Plots in Julia using the PGFPlots LaTeX package
Other
301 stars 40 forks source link

VLine and VBand with dates on the x-axis #292

Closed fipelle closed 1 year ago

fipelle commented 2 years ago

I am trying to use VLine / VBand on a line chart with dates on the x-axis. However, following the example in the documentation I get the error message: ERROR: MethodError: Cannot `convert` an object of type Date to an object of type Real. Are these shortcuts compatible with dates at all?

tpapp commented 2 years ago

Please include a minimum working example to replicate the issue.

tbeason commented 1 year ago

Interested in seeing if this can be fixed! Modifying an example from the docs:

using PGFPlotsX, Dates
dategrid = Date(2000,1,1):Day(1):Date(2000,12,31)
relative_irradiance(d) = (1 + 0.034*cospi(2*Dates.dayofyear(d)/365.25))

@pgf Axis(
    {
        date_coordinates_in = "x",
        x_tick_label_style = "{rotate=90}",
        xlabel = "date",
        ylabel = "relative solar irradiance",
    },
    VBand({ draw="none", fill="red", opacity = 0.5}, dategrid[50], dategrid[100]), # this line is new and will cause error
    Plot(
    {
        no_marks
    },
    Table(dategrid, relative_irradiance.(dategrid))))

And the error originates because of https://github.com/KristofferC/PGFPlotsX.jl/blob/366ccffcca413b5861c381564134855d823fb2c2/src/axiselements.jl#L727-L738

tbeason commented 1 year ago

Quick update here: on the backend it works. Convenience construct VBand just needs to be a bit a little bit more flexible.

@pgf Axis(
    {
        date_coordinates_in = "x",
        x_tick_label_style = "{rotate=-35}",
        xlabel = "date",
        ylabel = "relative solar irradiance",
        date_ZERO=minimum(dategrid)
            },
    [raw"\draw",
        { draw="none", fill="red", opacity = 0.5}, #options
     raw"({axis cs:2000-04-10,0}|-{rel axis cs:0,1}) rectangle ({axis cs:2000-07-10,0}|-{rel axis cs:0,0});"],
    Plot(
    {
        no_marks
    },
    Table(dategrid, relative_irradiance.(dategrid))))