4

I would like to restrict the domain of the x axis while using date coordinates. However, the following example (modified from pgfplots manual) will not compile since the domain boundaries for the third plot are not provided as floating point numbers:

\documentclass[11pt,a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{dateplot}

\begin{document}

\begin{tikzpicture}

\pgfplotstableread{
date account1 account2 account3
2008-01-03 60 1200 400
2008-02-06 120 1600 410
2008-03-15 -10 1600 410
2008-04-01 1800 500 410
2008-05-20 2300 500 410
2008-06-15 800 1920 410
}\myTable

\begin{axis}[
date coordinates in=x,
xticklabel={\day.\month.},
xlabel={2008},
yticklabel={\pgfmathprintnumber{\tick} EUR},
ylabel=Total credit,
ylabel style={yshift=10pt}]
\addplot table[x=date,y=account1] {\myTable};
\addplot table[x=date,y=account2] {\myTable};
\addplot table[x=date,y=account3,restrict x to domain={2008-01-03}:{2008-03-15}] {\myTable};
\end{axis}

\end{tikzpicture}

\end{document}

Do you have an idea on how to still achieve the desired effect of restricting the domain? I would like to avoid interfering with the data provided in the table.

P.S.: This question differs from Specify a domain when using date coordinates? which is on using soft clip. Using curly brackets for the domain boundaries as proposed therein does not work here.

1 Answer 1

4

The library dateplot would define x coord trafo so that it can translate dates (and times) to floating number. However, restrict x to domain does not include the information from x coord trafo. That is to say, PGFPLOTS will try to parse your date-inputs presuming they are floating number, ending up with the "cannot parse" error.

One might want to write a general restrict to domain-system that respects coord trafo-system. But one can also do the translation by hand, as follows

\pgfplotsset{
    restrict x to domain**/.code args={#1:#2}{
        \pgfkeysalso{/pgfplots/x coord trafo=#1}
        \let\numericxmin\pgfmathresult
        \pgfkeysalso{/pgfplots/x coord trafo=#2}
        \let\numericxmax\pgfmathresult
        \pgfkeysalso{/pgfplots/restrict x to domain={\numericxmin}:{\numericxmax}}
    }
}

An MWE reads

\documentclass[11pt,a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{dateplot}

\begin{document}

\begin{tikzpicture}

\pgfplotstableread{
date account1 account2 account3
2008-01-03 60 1200 400
2008-02-06 120 1600 410
2008-03-15 -10 1600 410
2008-04-01 1800 500 410
2008-05-20 2300 500 410
2008-06-15 800 1920 410
}\myTable

\pgfplotsset{
    restrict x to domain**/.code args={#1:#2}{
        \pgfkeysalso{/pgfplots/x coord trafo=#1}
        \let\numericxmin\pgfmathresult
        \pgfkeysalso{/pgfplots/x coord trafo=#2}
        \let\numericxmax\pgfmathresult
        \pgfkeysalso{/pgfplots/restrict x to domain={\numericxmin}:{\numericxmax}}
    }
}

\begin{axis}[
date coordinates in=x,
xticklabel={\day.\month.},
xlabel={2008},
yticklabel={\pgfmathprintnumber{\tick} EUR},
ylabel=Total credit,
ylabel style={yshift=10pt}]
\addplot table[x=date,y=account1] {\myTable};
\addplot table[x=date,y=account2] {\myTable};
\addplot table[x=date,y=account3,restrict x to domain**={2008-01-03}:{2008-03-15}] {\myTable};
\end{axis}

\end{tikzpicture}

\end{document}
1
  • This works perfectly fine - thank you very much!
    – LaTeX User
    Commented Jan 28, 2016 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .