I'm using dateplot so that I can use dates and times on the x axis. I want to restrict the plot over a range of dates, but am unable to do this...Does anyone know how? Thanks

Minimal example, mostly from page 333 of the pgf manual:




\begin{axis}[date coordinates in=x,
             x tick label style={rotate=90,anchor=near xticklabel},
             xticklabel=\day. \hour:\minute,
             date ZERO=2009-08-18]

%% I want to restrict the range of the plot over certain dates and times.

\addplot[restrict x to domain=2009-08-18 09:00:2009-08-18 15:00] 
    coordinates {
    (2009-08-18 09:00, 050)
    (2009-08-18 12:00, 100)
    (2009-08-18 15:00, 100)
    (2009-08-18 18:35, 100)
    (2009-08-18 21:30, 040)
    (2009-08-19 2:00, 020)
    (2009-08-19 3:00, 000)
    (2009-08-19 6:00, 035)

  • Try putting xmin=2009-08-18 09:00, xmax=2009-08-18 15:00, in the axis options. that should work. P.S. You generally want to put braces around dates, since they use a lot of reserved characters. Maybe `restrict x to domain={2009-08-18 09:00}:{2009-08-18 15:00} would work too. – Bibi Jan 18 '16 at 11:04
  • @Bibi Would you like to write up an answer? xmin/xmax works as you suggest. – Torbjørn T. Jan 24 '16 at 8:38
  • @user39678 You should consider starting to appreciate the time and effort people put into the answers to your quesitons by accepting them, otherwise less people are likely to help you in the future. – sodd Mar 1 '16 at 8:06

The line

restrict x to domain=2009-08-18 09:00:2009-08-18 15:00

is problematic (too many colons!). A solution is to put

xmin=2009-08-18 09:00, xmax=2009-08-18 15:00,

in the axis options.

Dates are usually problematic, since they use reserved characters (colon in particular). Sometimes you can circumvent the problem by putting braces around each date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.