带有 pgfplots 的多行 xticklabel

带有 pgfplots 的多行 xticklabel

我一直在尝试使用 PgfPlots 创建多行 xticklabels...

以下是示例代码(您可以轻松复制/粘贴并运行它):

\documentclass[a4paper, landscape, 10pt, table]{report}

\usepackage[yyyymmdd,hhmmss]{datetime}
\usepackage{array}
\usepackage[T1]{fontenc}
\usepackage{xstring}
\usepackage{ifthen}
\usepackage{readarray}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}

\begin{filecontents}{foo.csv}
Index;Start;Data
2;2020-12-23 16:57:47;412.0773923
3;2020-12-23 16:59:00;425.727314
4;2020-12-23 17:00:38;420.1815766
5;2020-12-23 17:01:32;427.3558494
6;2020-12-23 17:02:23;435.2599509
7;2020-12-23 17:03:16;422.8598639
8;2020-12-23 17:04:07;427.1698654
9;2020-12-23 17:05:01;426.6655106
10;2020-12-23 17:05:52;438.6186867
11;2020-12-23 17:06:46;436.1867562
12;2020-12-23 17:07:36;444.5109958
13;2020-12-23 17:08:57;446.4977636
14;2020-12-23 17:09:48;447.1165436
15;2020-12-23 17:10:41;454.8714161
16;2020-12-23 17:11:32;456.5764032
17;2020-12-23 17:12:26;444.5470121
18;2020-12-23 17:13:17;447.9966227
19;2020-12-23 17:14:11;442.5465669
20;2020-12-23 17:15:01;441.6672087
21;2020-12-23 17:15:55;456.5650485
22;2020-12-23 17:16:46;456.7810362
23;2020-12-23 17:17:40;445.482203
24;2020-12-23 17:18:31;449.7298524
25;2020-12-23 17:19:25;460.3123754
26;2020-12-23 17:20:15;461.9648056
27;2020-12-23 17:21:09;462.9873732
28;2020-12-23 17:22:00;456.2594798
29;2020-12-23 17:22:54;457.2116098
30;2020-12-23 17:23:45;459.9223908
31;2020-12-23 17:24:39;461.0290298
32;2020-12-23 17:25:29;459.9460694
33;2020-12-23 17:26:23;449.536673
34;2020-12-23 17:27:14;454.6444983
35;2020-12-23 17:28:08;462.1797674
36;2020-12-23 17:28:59;454.62287
37;2020-12-23 17:29:53;449.687945
38;2020-12-23 17:30:44;455.6168224
39;2020-12-23 17:31:38;464.9462649
40;2020-12-23 17:32:29;459.6818162
41;2020-12-23 17:33:23;463.5861117
42;2020-12-23 17:34:13;469.1470683
43;2020-12-23 17:35:07;466.2078869
44;2020-12-23 17:35:57;461.4459158
45;2020-12-23 17:36:50;456.2636787
46;2020-12-23 17:37:40;464.1252974
47;2020-12-23 17:38:34;464.7491836
48;2020-12-23 17:39:23;459.8645124
49;2020-12-23 17:40:16;455.7546055
50;2020-12-23 17:41:06;461.0567762
51;2020-12-23 17:41:59;464.6463023
52;2020-12-23 17:42:49;467.4802616
53;2020-12-23 17:43:42;459.3240784
54;2020-12-23 17:44:32;466.8316536
55;2020-12-23 17:45:24;469.5408074
56;2020-12-23 17:46:14;465.4033565
57;2020-12-23 17:47:07;473.51648
58;2020-12-23 17:47:56;462.3859205
59;2020-12-23 17:48:49;459.3232076
60;2020-12-23 17:49:39;471.4571371
61;2020-12-23 17:50:31;471.0410186
62;2020-12-23 17:51:21;462.7363749
63;2020-12-23 17:52:13;467.747319
64;2020-12-23 17:53:03;458.7079077
65;2020-12-23 17:53:55;463.3457445
66;2020-12-23 17:54:45;469.0103588
67;2020-12-23 17:55:37;463.3431042
68;2020-12-24 17:56:27;472.3724498
69;2020-12-24 17:57:19;468.9144406
70;2020-12-24 17:58:08;476.6813445
71;2020-12-24 17:59:01;471.2801298
72;2020-12-24 17:59:50;463.2278963
73;2020-12-24 18:00:42;466.0256436
74;2020-12-24 18:01:32;477.221236
75;2020-12-24 18:02:24;477.49307
76;2020-12-24 18:03:14;468.8653761
77;2020-12-24 18:04:06;466.8310255
78;2020-12-24 18:04:56;476.4680725
79;2020-12-24 18:05:48;472.616772
80;2020-12-24 18:06:38;467.5842863
81;2020-12-24 18:07:30;470.9433635
82;2020-12-24 18:08:20;466.4518209
83;2020-12-24 18:09:12;474.2457634
84;2020-12-24 18:10:02;468.8689096
85;2020-12-24 18:10:55;474.6539176
86;2020-12-24 18:11:44;471.2339234
87;2020-12-24 18:12:37;469.0997411
88;2020-12-24 18:13:26;461.762462
89;2020-12-24 18:14:19;471.6879006
90;2020-12-24 18:15:09;469.0627948
91;2020-12-24 18:16:01;470.9495337
92;2020-12-24 18:16:51;478.7524877
93;2020-12-24 18:17:44;479.7872284
94;2020-12-24 18:18:34;482.4520557
95;2020-12-24 18:19:27;470.1152276
96;2020-12-24 18:20:16;480.5053191
97;2020-12-24 18:21:09;477.2644034
98;2020-12-24 18:21:59;480.6109115
99;2020-12-24 18:22:52;484.6098949
100;2020-12-24 20:23:42;478.444049
101;2020-12-24 20:24:36;481.6190172
102;2020-12-24 20:25:26;471.307398
103;2020-12-24 20:26:20;482.3291329
104;2020-12-24 20:27:10;471.2151328
105;2020-12-24 20:28:04;481.4354713
106;2020-12-24 20:28:55;481.6304485
107;2020-12-24 21:29:49;449.311133
\end{filecontents}

% Read measurements data
\pgfplotstableread[col sep=semicolon]{foo.csv}{\variables}

% Data
\begin{tikzpicture}

  % Bottom axis
  \begin{axis}[
      xmin=2, xmax=107,
      ymin=400, ymax=500,
      xtick distance=10,
      xtick pos=bottom,
      xtick align=outside,
      ytick pos=left,
      xlabel=Index,
      width=\textwidth,
      height=0.5\textwidth,
  ]
  \addplot[red] table [x={Index}, y={Data}] {\variables};
  \legend{Data}
  \end{axis}
  
  % Upper axis with date/time labels
  \begin{axis}[
    xmin=2, xmax=107,
    axis x line*= top,
    axis y line=none,
    xlabel=Date/Time,
    xtick distance=10,
    xtick={0,10,...,100},
    xtick align=outside,
    xticklabel={
      \pgfmathparse{int(round(\tick-2))}
      \pgfplotstablegetelem{\pgfmathresult}{Start}\of{\variables}
      \StrBefore{\pgfplotsretval}{ }[\date]
      \StrBehind{\pgfplotsretval}{ }[\time]
      \date \time
    },
    xticklabel style={align=center},
    grid=none,
    width=\textwidth,
    height=0.5\textwidth]
    \addplot [draw=none] table[x={Index},y={Data}] {\variables};
  \end{axis}
\end{tikzpicture}

\end{document}

以下是文本重叠导致的不良结果: 不良结果/重叠

我想在日期和时间之间添加新行。我试过:

...
\date\\\time
\date\\ \time
\date\newline\time
...

但它不能编译。

如果我尝试:

...
\date \\ some string
...

没问题,我得到了:

在此处输入图片描述

你能解释一下我做错了什么吗?为什么它不能与 \time 变量一起使用?

感谢您的帮助

答案1

似乎\time宏在换行符处“丢失”了,虽然我说不出具体原因。如果你在它\\起作用之后重复整个日期解析,即

xticklabel={%
  \pgfmathparse{int(round(\tick-2))}%
  \pgfplotstablegetelem{\pgfmathresult}{Start}\of{\variables}%
  \StrBefore{\pgfplotsretval}{ }[\date]%
  \date\\
  \pgfmathparse{int(round(\tick-2))}%
  \pgfplotstablegetelem{\pgfmathresult}{Start}\of{\variables}%
  \StrBehind{\pgfplotsretval}{ }[\time]\time%
},

在此处输入图片描述

\documentclass[a4paper, landscape, 10pt, table]{report}

\usepackage[yyyymmdd,hhmmss]{datetime}
\usepackage{array}
\usepackage[T1]{fontenc}
\usepackage{xstring}
\usepackage{ifthen}
\usepackage{readarray}
\usepackage{geometry}
% \usepackage{tikz}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz, so don't actually need to load those two explicitly
% \usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}

\begin{filecontents}{foo.csv}
Index;Start;Data
2;2020-12-23 16:57:47;412.0773923
3;2020-12-23 16:59:00;425.727314
4;2020-12-23 17:00:38;420.1815766
5;2020-12-23 17:01:32;427.3558494
6;2020-12-23 17:02:23;435.2599509
7;2020-12-23 17:03:16;422.8598639
8;2020-12-23 17:04:07;427.1698654
9;2020-12-23 17:05:01;426.6655106
10;2020-12-23 17:05:52;438.6186867
11;2020-12-23 17:06:46;436.1867562
12;2020-12-23 17:07:36;444.5109958
13;2020-12-23 17:08:57;446.4977636
14;2020-12-23 17:09:48;447.1165436
15;2020-12-23 17:10:41;454.8714161
16;2020-12-23 17:11:32;456.5764032
17;2020-12-23 17:12:26;444.5470121
18;2020-12-23 17:13:17;447.9966227
19;2020-12-23 17:14:11;442.5465669
20;2020-12-23 17:15:01;441.6672087
21;2020-12-23 17:15:55;456.5650485
22;2020-12-23 17:16:46;456.7810362
23;2020-12-23 17:17:40;445.482203
24;2020-12-23 17:18:31;449.7298524
25;2020-12-23 17:19:25;460.3123754
26;2020-12-23 17:20:15;461.9648056
27;2020-12-23 17:21:09;462.9873732
28;2020-12-23 17:22:00;456.2594798
29;2020-12-23 17:22:54;457.2116098
30;2020-12-23 17:23:45;459.9223908
31;2020-12-23 17:24:39;461.0290298
32;2020-12-23 17:25:29;459.9460694
33;2020-12-23 17:26:23;449.536673
34;2020-12-23 17:27:14;454.6444983
35;2020-12-23 17:28:08;462.1797674
36;2020-12-23 17:28:59;454.62287
37;2020-12-23 17:29:53;449.687945
38;2020-12-23 17:30:44;455.6168224
39;2020-12-23 17:31:38;464.9462649
40;2020-12-23 17:32:29;459.6818162
41;2020-12-23 17:33:23;463.5861117
42;2020-12-23 17:34:13;469.1470683
43;2020-12-23 17:35:07;466.2078869
44;2020-12-23 17:35:57;461.4459158
45;2020-12-23 17:36:50;456.2636787
46;2020-12-23 17:37:40;464.1252974
47;2020-12-23 17:38:34;464.7491836
48;2020-12-23 17:39:23;459.8645124
49;2020-12-23 17:40:16;455.7546055
50;2020-12-23 17:41:06;461.0567762
51;2020-12-23 17:41:59;464.6463023
52;2020-12-23 17:42:49;467.4802616
53;2020-12-23 17:43:42;459.3240784
54;2020-12-23 17:44:32;466.8316536
55;2020-12-23 17:45:24;469.5408074
56;2020-12-23 17:46:14;465.4033565
57;2020-12-23 17:47:07;473.51648
58;2020-12-23 17:47:56;462.3859205
59;2020-12-23 17:48:49;459.3232076
60;2020-12-23 17:49:39;471.4571371
61;2020-12-23 17:50:31;471.0410186
62;2020-12-23 17:51:21;462.7363749
63;2020-12-23 17:52:13;467.747319
64;2020-12-23 17:53:03;458.7079077
65;2020-12-23 17:53:55;463.3457445
66;2020-12-23 17:54:45;469.0103588
67;2020-12-23 17:55:37;463.3431042
68;2020-12-24 17:56:27;472.3724498
69;2020-12-24 17:57:19;468.9144406
70;2020-12-24 17:58:08;476.6813445
71;2020-12-24 17:59:01;471.2801298
72;2020-12-24 17:59:50;463.2278963
73;2020-12-24 18:00:42;466.0256436
74;2020-12-24 18:01:32;477.221236
75;2020-12-24 18:02:24;477.49307
76;2020-12-24 18:03:14;468.8653761
77;2020-12-24 18:04:06;466.8310255
78;2020-12-24 18:04:56;476.4680725
79;2020-12-24 18:05:48;472.616772
80;2020-12-24 18:06:38;467.5842863
81;2020-12-24 18:07:30;470.9433635
82;2020-12-24 18:08:20;466.4518209
83;2020-12-24 18:09:12;474.2457634
84;2020-12-24 18:10:02;468.8689096
85;2020-12-24 18:10:55;474.6539176
86;2020-12-24 18:11:44;471.2339234
87;2020-12-24 18:12:37;469.0997411
88;2020-12-24 18:13:26;461.762462
89;2020-12-24 18:14:19;471.6879006
90;2020-12-24 18:15:09;469.0627948
91;2020-12-24 18:16:01;470.9495337
92;2020-12-24 18:16:51;478.7524877
93;2020-12-24 18:17:44;479.7872284
94;2020-12-24 18:18:34;482.4520557
95;2020-12-24 18:19:27;470.1152276
96;2020-12-24 18:20:16;480.5053191
97;2020-12-24 18:21:09;477.2644034
98;2020-12-24 18:21:59;480.6109115
99;2020-12-24 18:22:52;484.6098949
100;2020-12-24 20:23:42;478.444049
101;2020-12-24 20:24:36;481.6190172
102;2020-12-24 20:25:26;471.307398
103;2020-12-24 20:26:20;482.3291329
104;2020-12-24 20:27:10;471.2151328
105;2020-12-24 20:28:04;481.4354713
106;2020-12-24 20:28:55;481.6304485
107;2020-12-24 21:29:49;449.311133
\end{filecontents}

% Read measurements data
\pgfplotstableread[col sep=semicolon]{foo.csv}{\variables}

% Data
\begin{tikzpicture}

  % Bottom axis
  \begin{axis}[
      xmin=2, xmax=107,
      ymin=400, ymax=500,
      xtick distance=10,
      xtick pos=bottom,
      xtick align=outside,
      ytick pos=left,
      xlabel=Index,
      width=\textwidth,
      height=0.5\textwidth,
  ]
  \addplot[red] table [x={Index}, y={Data}] {\variables};
  \legend{Data}
  \end{axis}
  
  % Upper axis with date/time labels
  \begin{axis}[
    xmin=2, xmax=107,
    axis x line*= top,
    axis y line=none,
    xlabel=Date/Time,
    xtick distance=10,
    xtick={0,10,...,100},
    xtick align=outside,
    xticklabel={%
      \pgfmathparse{int(round(\tick-2))}%
      \pgfplotstablegetelem{\pgfmathresult}{Start}\of{\variables}%
      \StrBefore{\pgfplotsretval}{ }[\date]%
      \date\\
      \pgfmathparse{int(round(\tick-2))}%
      \pgfplotstablegetelem{\pgfmathresult}{Start}\of{\variables}%
      \StrBehind{\pgfplotsretval}{ }[\time]\time%
    },
    xticklabel style={align=center},
    grid=none,
    width=\textwidth,
    height=0.5\textwidth]
    \addplot [draw=none] table[x={Index},y={Data}] {\variables};
  \end{axis}
\end{tikzpicture}

\end{document}

相关内容