補遺

補遺

時間を表示したいええ軸。このコードを開発しましたが、出力が表示されません。

\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}

答え1

ライブラリを使用しようとしているように見えますdateplotが、マニュアルに記載されているように、必要時間だけでなく日付も指定します。

ここに画像の説明を入力してください

date coordinates in=xまた、 ではなく である必要があることにも注意してくださいTime coordinates in=x。入力ファイルに日付を追加し、それに応じてxmin/ の値を調整した後xmax、次のようになります。

\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}

ここに画像の説明を入力してください

補遺

一方、シンボル1の答えをTikZ と pgfplots を使用して日付と時刻のプロットを作成する方法次に、以下のコードを参照してください。質問でこれについて言及していただければよかったのですが (ただし、frougon の要求どおり、さらに情報を提供するまで待つこともできたと思います...)。

スタイルに正しい名前を使用していることを確認する必要があります。シンボル 1 のコードは を定義しtime coordinates in、 を使用しましたTime coordinates in。これは大文字と小文字が区別されるため、スタイルの定義と使用の両方で同じ大文字と小文字を使用する必要があります。

凡例が空であることに気付きました。Stefan Pinnowは、ycombプロットの凡例をより便利にする方法を説明しています。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}

答え2

frougon が述べているように、あなたのコードは完成には程遠いです。おそらく、次のコードは正しい方向に向かっているでしょう。

\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}

ここに画像の説明を入力してください

関連情報