1

I want to do a graph like below. Showing the duration of an intervention in a country. enter image description here

generated by this code:

\begin{tikzpicture}
\begin{axis}[
    date coordinates in=x,
    xticklabel={\day/\month},
    xticklabel style={rotate=90,anchor=near xticklabel},
    axis y line=none,
    axis x line=bottom,
    xmin = 2020-01-25
    ]
\addplot +[color=black,no marks,ultra thick]
table[x=date,y=HUN] {tables/policies_time.txt} node[left,pos=0] {HUN};
\addplot +[color=black,no marks,ultra thick]
table[x=date,y=DEU] {tables/policies_time.txt} node[left,pos=0] {DEU};
\addplot +[color=black,no marks,ultra thick]
table[x=date,y=AUT] {tables/policies_time.txt} node[left,pos=0] {AUT};
\end{axis}
\end{tikzpicture}

I want to use a for loop, because I have a much bigger set of countries. Therefor I make a loop and replace the labels "HUN", "DEU" and "AUT" by the running variable \isos in both the table[] part and the labeling node part. But this time the labels give an error, and do not appear.

Here is my code for the looped version:

\begin{tikzpicture}
\begin{axis}[
        date coordinates in=x,
        xticklabel={\day/\month},
        xticklabel style={rotate=90,anchor=near xticklabel},
        axis y line=none,
        axis x line=bottom,
        xmin = 2020-01-25
        ]
\makeatletter
\@for\isos:=HUN,DEU,AUT\do{
    \addplot +[color=black,no marks,ultra thick] table[x=date,y=\isos]    {tables/policies_time.txt}
    node[left,pos=0] {\isos};
    }
\makeatother
\end{axis}
\end{tikzpicture}

Do you have any suggestions, how to solve this problem?

Thank you,

Oliver

  • Welome to TeX.SX! It would be great if you could provide a few lines from tables/policies_time.txt. Also, please provide a minimal working example (MWE) starting with \documentclass and ending with \end{document} instead of just a snippet. – Jasper Habicht Jul 29 '21 at 16:10
  • Related: https://tex.stackexchange.com/q/264168/47927 – Jasper Habicht Jul 29 '21 at 16:29

1 Answers1

0

As this answer suggests, you will get into expansion trouble if you combine \foreach (or \@for) with \addplot in such a way. However, you can use \pgfplotsinvokeforeach for this:

\documentclass[border=2mm]{standalone}

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

\begin{document}

\begin{tikzpicture} \begin{axis}[ date coordinates in=x, xticklabel={\day/\month}, xticklabel style={rotate=90,anchor=near xticklabel}, axis y line=none, axis x line=bottom, xmin = 2020-01-25 ] \pgfplotsinvokeforeach{HUN,DEU,AUT}{ \addplot +[color=black,no marks,ultra thick] table[x=date,y=#1] {tables/policies_time.txt} node[left,pos=0] {#1}; } \end{axis} \end{tikzpicture}

\end{document}

As you did not provide any data to feed this graph, I cannot really test this.

  • Hey Jasper! Thanks a lot for this quick answer. Works perfectly. And I have just found the same question and solution from 9 years ago https://tex.stackexchange.com/questions/50477/using-foreach-loop-variable-as-node-label-in-pgfplots – Oliver Racz Jul 30 '21 at 11:25