我制作了 24 小时图表(使用不同的 y 轴)。到目前为止一切顺利。
但 x-axe 没有按照我想要的方式显示。
1/ 时间显示不正确,05:59:00 而不是 06:00:00
2/ X 斧应该在 24:00:00 停止(现在它可以走得更远)。
任何帮助,将不胜感激。
我的代码:
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{comment}
\usepackage{filecontents}
\usepackage{geometry}
\usepackage{tikz}
\usepgfplotslibrary{dateplot}
\def\removeleadingzeros#1{\if0#1 \expandafter\else#1\fi}
\def\transformtime#1:#2:#3!{
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
%\pgfmathparse{#1*3600-\pgfkeysvalueof{/pgfplots/timeplot zero}*3600+#2*60+#3}
\pgfmathparse{\removeleadingzeros#1*3600-\pgfkeysvalueof{/pgfplots/timeplot zero}*3600+\removeleadingzeros#2*60+\removeleadingzeros#3}
\pgfkeys{/pgf/fpu=false}
}
\pgfplotsset{
timeplot zero/.initial=0,
timeplot/.style={
x coord trafo/.code={\expandafter\transformtime##1!},
x coord inv trafo/.code={%
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\hours{floor(##1/3600)+\pgfkeysvalueof{/pgfplots/timeplot zero}}
\pgfmathsetmacro\minutes{floor((##1-(\hours-\pgfkeysvalueof{/pgfplots/timeplot zero})*3600)/60)}
\pgfmathsetmacro\seconds{##1-floor(##1/60)*60}
\def\pgfmathresult{\pgfmathparse{mod(\hours,60)<10?"0":{},int(mod(\hours,60))}\pgfmathresult:\pgfmathparse{mod(\minutes,60)<10?"0":{},int(mod(\minutes,60))}\pgfmathresult:\pgfmathparse{mod(\seconds,60)<10?"0":{},int(mod(\seconds,60))}\pgfmathresult}
\pgfkeys{/pgf/fpu=false}
},
scaled x ticks=false,
xticklabel=\tick
}
}
\begin{document}
\begin{filecontents*}{boom.dat}
tijd diameter
00:00:00 656.405681
00:15:00 656.410403
00:30:00 656.415722
00:45:00 656.419879
01:00:00 656.422925
01:15:00 656.425762
01:30:00 656.429439
01:45:00 656.432858
02:00:00 656.435443
02:15:00 656.437564
02:30:00 656.438722
02:45:00 656.441318
03:00:00 656.443924
03:15:00 656.446608
03:30:00 656.449341
03:45:00 656.450762
04:00:00 656.451835
04:15:00 656.453
04:30:00 656.454701
04:45:00 656.456525
05:00:00 656.458294
05:15:00 656.460722
05:30:00 656.462619
05:45:00 656.464791
06:00:00 656.466648
06:15:00 656.467714
06:30:00 656.469632
06:45:00 656.470793
07:00:00 656.471614
07:15:00 656.47177
07:30:00 656.472144
07:45:00 656.472204
08:00:00 656.472353
08:15:00 656.471947
08:30:00 656.471044
08:45:00 656.467682
09:00:00 656.45527
09:15:00 656.438903
09:30:00 656.422273
09:45:00 656.41521
10:00:00 656.408804
10:15:00 656.402313
10:30:00 656.393826
10:45:00 656.385612
11:00:00 656.378146
11:15:00 656.372256
11:30:00 656.367282
11:45:00 656.361468
12:00:00 656.355557
12:15:00 656.350256
12:30:00 656.343752
12:45:00 656.337133
13:00:00 656.332531
13:15:00 656.32958
13:30:00 656.328682
13:45:00 656.327219
14:00:00 656.321448
14:15:00 656.315754
14:30:00 656.312755
14:45:00 656.311984
15:00:00 656.31289
15:15:00 656.313705
15:30:00 656.316576
15:45:00 656.323175
16:00:00 656.327068
16:15:00 656.326734
16:30:00 656.329307
16:45:00 656.34159
17:00:00 656.344922
17:15:00 656.344023
17:30:00 656.34417
17:45:00 656.344636
18:00:00 656.345115
18:15:00 656.344474
18:30:00 656.344168
18:45:00 656.34317
19:00:00 656.343425
19:15:00 656.343974
19:30:00 656.345429
19:45:00 656.351991
20:00:00 656.361141
20:15:00 656.366497
20:30:00 656.374289
20:45:00 656.381799
21:00:00 656.389487
21:15:00 656.395972
21:30:00 656.402692
21:45:00 656.408891
22:00:00 656.416834
22:15:00 656.422846
22:30:00 656.428715
22:45:00 656.435375
23:00:00 656.441634
23:15:00 656.445468
23:30:00 656.449594
23:45:00 656.452806
\end{filecontents*}
\begin{filecontents*}{sapstroom.dat}
tijd sapstroom
00:00:00 4.85408
00:15:00 4.57286
00:30:00 4.39551
00:45:00 4.4178
01:00:00 4.04949
01:15:00 3.77214
01:30:00 3.62084
01:45:00 3.46315
02:00:00 3.28671
02:15:00 3.21523
02:30:00 3.13485
02:45:00 2.97071
03:00:00 2.88493
03:15:00 2.96935
03:30:00 2.48116
03:45:00 2.45723
04:00:00 2.38146
04:15:00 2.37392
04:30:00 2.48235
04:45:00 2.28593
05:00:00 2.23122
05:15:00 2.20008
05:30:00 2.13761
05:45:00 2.05941
06:00:00 1.95399
06:15:00 1.90721
06:30:00 1.79834
06:45:00 1.77564
07:00:00 1.86757
07:15:00 2.13706
07:30:00 2.75334
07:45:00 3.49142
08:00:00 4.59272
08:15:00 6.20182
08:30:00 8.13804
08:45:00 10.2965
09:00:00 12.6352
09:15:00 14.649
09:30:00 16.9704
09:45:00 18.306
10:00:00 19.6905
10:15:00 20.6927
10:30:00 21.2783
10:45:00 21.9715
11:00:00 22.2801
11:15:00 22.4729
11:30:00 22.697
11:45:00 22.5211
12:00:00 22.9484
12:15:00 22.9198
12:30:00 22.9355
12:45:00 23.1047
13:00:00 23.0004
13:15:00 23.0681
13:30:00 22.7262
13:45:00 22.7039
14:00:00 23.0067
14:15:00 23.2884
14:30:00 23.2097
14:45:00 22.9358
15:00:00 22.478
15:15:00 21.9537
15:30:00 21.1719
15:45:00 21.2557
16:00:00 21.1705
16:15:00 22.4717
16:30:00 21.3172
16:45:00 19.8637
17:00:00 20.242
17:15:00 21.574
17:30:00 21.7862
17:45:00 21.4883
18:00:00 21.0291
18:15:00 20.684
18:30:00 20.9946
18:45:00 21.6095
19:00:00 21.0845
19:15:00 20.2188
19:30:00 19.1834
19:45:00 18.3425
20:00:00 17.5752
20:15:00 17.0377
20:30:00 15.6622
20:45:00 14.5367
21:00:00 13.3425
21:15:00 11.8389
21:30:00 10.4741
21:45:00 9.53448
22:00:00 8.7462
22:15:00 7.98585
22:30:00 7.51326
22:45:00 7.01831
23:00:00 6.48857
23:15:00 5.97139
23:30:00 5.63499
23:45:00 5.26984
\end{filecontents*}
\begin{tikzpicture}
\begin{axis}[
%axis lines=center,
xmin=00:00:00, ymax=24:00:00,
ymin=656.3, ymax=656.5,
title=Boomdiagram,
xlabel=Tijdstip,
ylabel=Diameter (mm),
ylabel near ticks,
xlabel near ticks,
timeplot, timeplot zero=0,
xtick={00:00:00,06:00:00,12:00:00,18:00:00,24:00:00},
%minor xtick={00:15:00,00:30:00,00:45:00,01:00:00,01:15:00,01:30:00,01:45:00},
minor ytick={656.1,656.32,...,656.48},
grid=both,
width=\linewidth,
%height=8cm,
y tick label style={/pgf/number format/fixed, /pgf/number format/precision=2, /pgf/number format/fixed zerofill},
tick label style={font=\small},
x tick label style={rotate=-90},
]
\addplot[blue,mark=*,mark size=1] table [x=tijd, y=diameter] {boom.dat}; \label{plot_one}
\addlegendentry{Diameter}
\end{axis}
\begin{axis}[
%axis lines=center,
xmin=00:00:00, ymax=24:00:00,
ymin=0, ymax=25,
title=Boomdiagram,
xlabel=Tijdstip,
axis y line*=right,
ylabel=Sapstroom,
ylabel near ticks,
xlabel near ticks,
timeplot, timeplot zero=0,
xtick={00:00:00,06:00:00,12:00:00,18:00:00,24:00:00},
grid=both,
width=\linewidth,
%height=8cm,
x tick label style={rotate=-90},
tick label style={font=\small},
]
\addplot[red,mark=triangle*] table [x=tijd, y=sapstroom] {sapstroom.dat};\label{plot_two}
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{Sapstroom}
\addlegendentry{Boomdiameter}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
在修正了拼写错误xmax
(输入了ymax
两次)并做了一点小改动(我将 xticks 改为xtick={00:00:00,06:00:00.5,12:00:00,18:00:00.5,24:00:00}
,即增加了 6am 和 6pm 0.5 秒;我猜是某个地方出现了舍入问题)之后,我得到了
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{comment}
\usepackage{filecontents}
\usepackage{geometry}
\usepackage{tikz}
\usepgfplotslibrary{dateplot}
\def\removeleadingzeros#1{\if0#1 \expandafter\else#1\fi}
\def\transformtime#1:#2:#3!{
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
%\pgfmathparse{#1*3600-\pgfkeysvalueof{/pgfplots/timeplot zero}*3600+#2*60+#3}
\pgfmathparse{\removeleadingzeros#1*3600-\pgfkeysvalueof{/pgfplots/timeplot zero}*3600+\removeleadingzeros#2*60+\removeleadingzeros#3}
\pgfkeys{/pgf/fpu=false}
}
\pgfplotsset{
timeplot zero/.initial=0,
timeplot/.style={
x coord trafo/.code={\expandafter\transformtime##1!},
x coord inv trafo/.code={%
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\hours{floor(##1/3600)+\pgfkeysvalueof{/pgfplots/timeplot zero}}
\pgfmathsetmacro\minutes{floor((##1-(\hours-\pgfkeysvalueof{/pgfplots/timeplot zero})*3600)/60)}
\pgfmathsetmacro\seconds{##1-floor(##1/60)*60}
\def\pgfmathresult{\pgfmathparse{mod(\hours,60)<10?"0":{},int(mod(\hours,60))}\pgfmathresult:\pgfmathparse{mod(\minutes,60)<10?"0":{},int(mod(\minutes,60))}\pgfmathresult:\pgfmathparse{mod(\seconds,60)<10?"0":{},int(mod(\seconds,60))}\pgfmathresult}
\pgfkeys{/pgf/fpu=false}
},
scaled x ticks=false,
xticklabel=\tick
}
}
\begin{document}
\begin{filecontents*}{boom.dat}
tijd diameter
00:00:00 656.405681
00:15:00 656.410403
00:30:00 656.415722
00:45:00 656.419879
01:00:00 656.422925
01:15:00 656.425762
01:30:00 656.429439
01:45:00 656.432858
02:00:00 656.435443
02:15:00 656.437564
02:30:00 656.438722
02:45:00 656.441318
03:00:00 656.443924
03:15:00 656.446608
03:30:00 656.449341
03:45:00 656.450762
04:00:00 656.451835
04:15:00 656.453
04:30:00 656.454701
04:45:00 656.456525
05:00:00 656.458294
05:15:00 656.460722
05:30:00 656.462619
05:45:00 656.464791
06:00:00 656.466648
06:15:00 656.467714
06:30:00 656.469632
06:45:00 656.470793
07:00:00 656.471614
07:15:00 656.47177
07:30:00 656.472144
07:45:00 656.472204
08:00:00 656.472353
08:15:00 656.471947
08:30:00 656.471044
08:45:00 656.467682
09:00:00 656.45527
09:15:00 656.438903
09:30:00 656.422273
09:45:00 656.41521
10:00:00 656.408804
10:15:00 656.402313
10:30:00 656.393826
10:45:00 656.385612
11:00:00 656.378146
11:15:00 656.372256
11:30:00 656.367282
11:45:00 656.361468
12:00:00 656.355557
12:15:00 656.350256
12:30:00 656.343752
12:45:00 656.337133
13:00:00 656.332531
13:15:00 656.32958
13:30:00 656.328682
13:45:00 656.327219
14:00:00 656.321448
14:15:00 656.315754
14:30:00 656.312755
14:45:00 656.311984
15:00:00 656.31289
15:15:00 656.313705
15:30:00 656.316576
15:45:00 656.323175
16:00:00 656.327068
16:15:00 656.326734
16:30:00 656.329307
16:45:00 656.34159
17:00:00 656.344922
17:15:00 656.344023
17:30:00 656.34417
17:45:00 656.344636
18:00:00 656.345115
18:15:00 656.344474
18:30:00 656.344168
18:45:00 656.34317
19:00:00 656.343425
19:15:00 656.343974
19:30:00 656.345429
19:45:00 656.351991
20:00:00 656.361141
20:15:00 656.366497
20:30:00 656.374289
20:45:00 656.381799
21:00:00 656.389487
21:15:00 656.395972
21:30:00 656.402692
21:45:00 656.408891
22:00:00 656.416834
22:15:00 656.422846
22:30:00 656.428715
22:45:00 656.435375
23:00:00 656.441634
23:15:00 656.445468
23:30:00 656.449594
23:45:00 656.452806
\end{filecontents*}
\begin{filecontents*}{sapstroom.dat}
tijd sapstroom
00:00:00 4.85408
00:15:00 4.57286
00:30:00 4.39551
00:45:00 4.4178
01:00:00 4.04949
01:15:00 3.77214
01:30:00 3.62084
01:45:00 3.46315
02:00:00 3.28671
02:15:00 3.21523
02:30:00 3.13485
02:45:00 2.97071
03:00:00 2.88493
03:15:00 2.96935
03:30:00 2.48116
03:45:00 2.45723
04:00:00 2.38146
04:15:00 2.37392
04:30:00 2.48235
04:45:00 2.28593
05:00:00 2.23122
05:15:00 2.20008
05:30:00 2.13761
05:45:00 2.05941
06:00:00 1.95399
06:15:00 1.90721
06:30:00 1.79834
06:45:00 1.77564
07:00:00 1.86757
07:15:00 2.13706
07:30:00 2.75334
07:45:00 3.49142
08:00:00 4.59272
08:15:00 6.20182
08:30:00 8.13804
08:45:00 10.2965
09:00:00 12.6352
09:15:00 14.649
09:30:00 16.9704
09:45:00 18.306
10:00:00 19.6905
10:15:00 20.6927
10:30:00 21.2783
10:45:00 21.9715
11:00:00 22.2801
11:15:00 22.4729
11:30:00 22.697
11:45:00 22.5211
12:00:00 22.9484
12:15:00 22.9198
12:30:00 22.9355
12:45:00 23.1047
13:00:00 23.0004
13:15:00 23.0681
13:30:00 22.7262
13:45:00 22.7039
14:00:00 23.0067
14:15:00 23.2884
14:30:00 23.2097
14:45:00 22.9358
15:00:00 22.478
15:15:00 21.9537
15:30:00 21.1719
15:45:00 21.2557
16:00:00 21.1705
16:15:00 22.4717
16:30:00 21.3172
16:45:00 19.8637
17:00:00 20.242
17:15:00 21.574
17:30:00 21.7862
17:45:00 21.4883
18:00:00 21.0291
18:15:00 20.684
18:30:00 20.9946
18:45:00 21.6095
19:00:00 21.0845
19:15:00 20.2188
19:30:00 19.1834
19:45:00 18.3425
20:00:00 17.5752
20:15:00 17.0377
20:30:00 15.6622
20:45:00 14.5367
21:00:00 13.3425
21:15:00 11.8389
21:30:00 10.4741
21:45:00 9.53448
22:00:00 8.7462
22:15:00 7.98585
22:30:00 7.51326
22:45:00 7.01831
23:00:00 6.48857
23:15:00 5.97139
23:30:00 5.63499
23:45:00 5.26984
\end{filecontents*}
\begin{tikzpicture}
\begin{axis}[
%axis lines=center,
xmin=00:00:00, xmax=24:00:00,
ymin=656.3, ymax=656.5,
title=Boomdiagram,
xlabel=Tijdstip,
ylabel=Diameter (mm),
ylabel near ticks,
xlabel near ticks,
timeplot, timeplot zero=0,
xtick={00:00:00,06:00:00.5,12:00:00,18:00:00.5,24:00:00},
%minor xtick={00:15:00,00:30:00,00:45:00,01:00:00,01:15:00,01:30:00,01:45:00},
minor ytick={656.1,656.32,...,656.48},
grid=both,
width=\linewidth,
%height=8cm,
y tick label style={/pgf/number format/fixed, /pgf/number format/precision=2, /pgf/number format/fixed zerofill},
tick label style={font=\small},
x tick label style={rotate=-90},
]
\addplot[blue,mark=*,mark size=1] table [x=tijd, y=diameter] {boom.dat}; \label{plot_one}
\addlegendentry{Diameter}
\end{axis}
\begin{axis}[
%axis lines=center,
xmin=00:00:00, xmax=24:00:00,
ymin=0, ymax=25,
title=Boomdiagram,
xlabel=Tijdstip,
axis y line*=right,
ylabel=Sapstroom,
ylabel near ticks,
xlabel near ticks,
timeplot, timeplot zero=0,
xtick={00:00:00,06:00:00.5,12:00:00,18:00:00.5,24:00:00},
grid=both,
width=\linewidth,
%height=8cm,
x tick label style={rotate=-90},
tick label style={font=\small},
]
\addplot[red,mark=triangle*] table [x=tijd, y=sapstroom] {sapstroom.dat};\label{plot_two}
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{Sapstroom}
\addlegendentry{Boomdiameter}
\end{axis}
\end{tikzpicture}
\end{document}