2

I want to display time on the y axis. I have developed this code, but it is not showing any output.

\usepackage{filecontents}
\begin{filecontents}{mwe.csv}
Time,a,b
 00:00,0,10
 10:30,6,4
 11:30,5,5
 12:30,3,7
 13:30,2,8
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
Time coordinates in=x, 
xticklabel={\begin{turn}{45}\hour.\minute \end{turn}\\},
x tick label style={align=center},
ymajorgrids,
xmin={00:00},
xmax={21:30},
ymin=00:00,
legend pos=north west,]
\addplot [ycomb,color=blue] table [col sep=comma,trim cells=true,y=a] {mwe.csv}; \addlegendentry{a}
\addplot [ycomb,color=red]  table [col sep=comma,trim cells=true,y=b] {mwe.csv}; \addlegendentry{b}
\end{axis}
\end{tikzpicture}
\end{document}
frougon
  • 24,283
  • 1
  • 32
  • 55
  • 2
    Welcome to TeX.SE. Please turn your code into something that compiles (it is missing a \documentclass, you don't need \usepackage{filecontents} with recent LaTeX, you are missing other packages though...). Thank you. – frougon Mar 27 '20 at 11:35

2 Answers2

4

It sort of looks like you're trying to use the dateplot library, but as described in the manual, you need to specify a date, not just the time:

enter image description here

Note also that it should be date coordinates in=x, not Time coordinates in=x. After adding a date to the input file, and adjusting the values for xmin/xmax accordingly:

\begin{filecontents}{mwe.csv}
Time,a,b
2020-1-1 00:00,0,10
2020-1-1 10:30,6,4
2020-1-1 11:30,5,5
2020-1-1 12:30,3,7
2020-1-1 13:30,2,8
\end{filecontents}

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x, 
xticklabel={\hour.\minute},
x tick label style={align=center,rotate=45},
ymajorgrids,
xmin={2020-1-1 00:00},
xmax={2020-1-1 21:30},
legend pos=north west
]

\addplot [ycomb,color=blue] table [col sep=comma,y=a] {mwe.csv};
 \addlegendentry{a}
\addplot [ycomb,color=red]  table [col sep=comma,y=b] {mwe.csv};
 \addlegendentry{b}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Addendum

If, on the other hand, you're trying to use Symbol 1's answer at How to create a date and time plot with TikZ and pgfplots then see the code below. It would have been great if you had mentioned this in the question (though I suppose I could have waited until you supplied more information, as requested by frougon ...).

You still need to make sure you use the right name for the style, Symbol 1's code defines time coordinates in, you used Time coordinates in. This is case sensitive, so you need to use the same case in both style definition and use.

I do note that the legend is empty. Stefan Pinnow demonstrates how to get a more useful legend for ycomb plots in https://tex.stackexchange.com/a/357469/.

\begin{filecontents*}{mwe.csv}
Time,a,b
00:00,0,10
10:30,6,4
11:30,5,5
12:30,3,7
13:30,2,8
\end{filecontents*}

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}

% the following is from https://tex.stackexchange.com/a/288225/
\def\pgfplotslibdateplothour:minutetofloat#1:#2.{
    \pgfmathparse{#1+#2/60}
}
\def\pgfplotslibdateplofloattothour:minute#1.{
    \pgfmathsetmacro\hour{int(floor(#1))}
    \pgfmathsetmacro\minute{int((#1-\hour)*60)}
    \ifnum\hour<10\edef\hour{0\hour}\fi
    \ifnum\minute<10\edef\minute{0\minute}\fi
}
\pgfplotsset{
    /pgfplots/time coordinates in/.code={%
        \pgfkeysdef{/pgfplots/#1 coord trafo}{%
            \pgfplotslibdateplothour:minutetofloat##1.
        }
        \pgfkeysdef{/pgfplots/#1 coord inv trafo}{
            \pgfplotslibdateplofloattothour:minute##1.
        }
    }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
time coordinates in=x,
xticklabel={\hour.\minute},
x tick label style={align=center,rotate=45},
ymajorgrids,
xmin={00:00},
xmax={21:30},
legend pos=north west,
]
\addplot [ycomb, draw=blue] table [col sep=comma,trim cells=true,y=a] {mwe.csv};
\addlegendentry{a}

\addplot [ycomb,color=red]  table [col sep=comma,trim cells=true,y=b] {mwe.csv};
\addlegendentry{b}

\end{axis}
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688
3

As frougon mentions, your code is far from complete. Maybe the following goes in the right direction.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{dateplot}
\usepackage{filecontents}
\begin{filecontents}{mwe.csv}
Time,a,b
 00:00,0,10
 10:30,6,4
 11:30,5,5
 12:30,3,7
 13:30,2,8
\end{filecontents}

\usepackage{xfp}
% from https://tex.stackexchange.com/a/445564/121799
\def\marmottimeparseaux#1:#2xxx{#1* 60 + #2}
\ExplSyntaxOn
\cs_new:Nn \marmot_timeparse:n { \marmottimeparseaux #1xxx }
\newcommand\TimeParse[1]{\exp_args:Nf\marmot_timeparse:n {#1}}
\ExplSyntaxOff
\pgfplotsset{
    /pgfplots/ycomb legend/.style={
        /pgfplots/legend image code/.code={%
            \path (0cm,0cm) (0.6cm,0.3cm);  
            \draw [#1] (0.3cm,-0.1cm) -- (0.3cm,0.2cm);},
    },
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[ycomb,ycomb legend,
xticklabel={\pgfmathtruncatemacro{\myhour}{\tick/60}%
\pgfmathtruncatemacro{\myminute}{\tick-\myhour*60}%
\myhour:\myminute},
x tick label style={align=center},
ymajorgrids,
xmin=\TimeParse{00:-10},
xmax=\TimeParse{21:30},
ymin=0,
legend pos=north west,]
\addplot [color=blue,xshift=-2pt] table [col sep=comma,y=a,
    x expr=\TimeParse{\thisrow{Time}}] {mwe.csv};   
\addlegendentry{a}
\addplot [ycomb,color=red,xshift=2pt]  table [col sep=comma,trim cells=true,y=b,
x expr=\TimeParse{\thisrow{Time}}] {mwe.csv}; 
\addlegendentry{b}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • +1 for using expl3 :-)) (would xxxitis be caused by a mutation of the zzzitis pathogen?) – frougon Mar 27 '20 at 14:10
  • @frougon Like this? ;-) –  Mar 27 '20 at 14:12
  • Pfff, blasphemy! I should retract my upvote! :) – frougon Mar 27 '20 at 14:13
  • @frougon The xxx is from Ulrike's answer that is quoted above. I wouldn't use it, but what saves it is the name of the macro. ;-) –  Mar 27 '20 at 14:25
  • Ha! Regarding the comment there about using \def to define the auxiliary macro: in your file (from this question), the following works: \ExplSyntaxOn \use:e { \exp_not:n { \cs_new:Npn \__marmot_timeparse_aux:w #1 } \char_generate:nn { \: } { 12 } \exp_not:n { #2 \q_stop { #1* 60 + #2 } } } \cs_new:Nn \marmot_timeparse:n { __marmot_timeparse_aux:w #1 \q_stop }.... The difficulty comes from the fact that we want a catcode-12:in the parameter text of the auxiliary macro, but after\ExplSyntaxOn,:has catcode 11. In this case, the\def` is more readable! – frougon Mar 27 '20 at 14:46