![如何在 pgfplots 中强制 .dat 文件的 x 轴值?](https://linux22.com/image/450580/%E5%A6%82%E4%BD%95%E5%9C%A8%20pgfplots%20%E4%B8%AD%E5%BC%BA%E5%88%B6%20.dat%20%E6%96%87%E4%BB%B6%E7%9A%84%20x%20%E8%BD%B4%E5%80%BC%EF%BC%9F.png)
我试图强迫我的X轴定义。我有两个 .dat 文件,它们定义在X轴从 0 到 1,但它们代表小时。0 代表 0 小时,1 代表 13 小时。如何使用 .dat 文件的信息将 x 轴定义为从 0 到 13 小时?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlargelimits=false,
ymin=30, ymax=110,
xmin=0, xmax=1, %<- I've added this parameter to generate my MWE
xtick={0,0.1,...,1},
ytick={30,40,...,110},
ylabel={Temperature [°$\mathrm{C}$]},
xlabel={Time [h]},
ymajorgrids=true,
grid style=dashed,
scaled ticks=false,
x tick label style={rotate=90},
]
\addplot+[
mark=triangle,
mark size=1pt]
table[meta=Temperatura]{A.dat};
\addlegendentry{A}
\addplot+[
mark=+,
mark size=1pt]
table[meta=Temperatura]{B.dat};
\addlegendentry{B}
\end{axis}
\end{tikzpicture}
\end{document}
B.dat文件:
Time Temperature
0 100
0.00128 98.50
0.00256 97.89
0.00641 96.82
0.01282 95.54
0.02564 93.68
0.03846 92.23
0.05128 90.91
0.0641 89.69
0.07692 88.51
0.08974 87.38
0.10256 86.28
0.11538 85.21
0.1282 84.16
0.14102 83.13
0.15385 82.14
0.16667 81.18
0.17949 80.2
0.19231 79.26
0.20512 78.34
0.21795 77.43
0.23077 76.55
0.24359 75.68
0.25641 74.82
0.26923 73.99
0.28205 73.17
0.29487 72.36
0.30769 71.57
0.32051 70.8
0.33333 70.03
0.34615 69.28
0.35897 68.54
0.37179 67.83
0.38462 67.12
0.39744 66.43
0.41026 65.74
0.42308 65.07
0.4359 64.41
0.44871 63.77
0.46154 63.14
0.47436 62.52
0.48718 61.91
0.5 61.3
0.51282 60.71
0.52564 60.13
0.53846 59.56
0.55128 59
0.5641 58.45
0.57692 57.81
0.58974 57.38
0.60256 56.86
0.61538 56.35
0.62821 55.84
0.64102 55.35
0.65385 54.87
0.66667 54.38
0.67949 53.91
0.69231 53.44
0.70513 53
0.71795 52.55
0.73077 52.12
0.74359 51.69
0.75641 51.27
0.76923 50.85
0.78205 50.44
0.79487 50.04
0.80769 49.65
0.82051 49.26
0.83333 48.88
0.84615 48.51
0.85897 48.14
0.87179 47.78
0.88462 47.42
0.89744 47.07
0.91026 46.73
0.92307 46.39
0.935897 46.05
0.94872 45.73
0.96154 45.41
0.97436 45.1
0.98718 44.78
1 44.48
A.dat文件:
Time Temperature
0 100
0.00128 99.89
0.00256 99.67
0.00641 99.06
0.01282 98.15
0.02564 96.55
0.03846 95.09
0.05128 93.73
0.0641 92.46
0.07692 91.26
0.08974 90.12
0.10256 89.03
0.11538 87.99
0.1282 87
0.14102 86.06
0.15385 85.15
0.16667 84.27
0.17949 83.41
0.19231 82.59
0.20512 81.80
0.21795 81.02
0.23077 80.26
0.24359 79.54
0.25641 78.83
0.26923 78.13
0.28205 77.46
0.29487 76.81
0.30769 76.17
0.32051 75.55
0.33333 74.95
0.34615 74.37
0.35897 73.79
0.37179 73.24
0.38462 72.69
0.39744 72.16
0.41026 71.64
0.42308 71.13
0.4359 70.65
0.44871 70.16
0.46154 69.68
0.47436 69.22
0.48718 68.77
0.5 68.33
0.51282 67.9
0.52564 67.47
0.53846 67.06
0.55128 66.65
0.5641 66.26
0.57692 65.87
0.58974 65.48
0.60256 65.12
0.61538 64.75
0.62821 64.39
0.64102 64.04
0.65385 63.70
0.66667 63.36
0.67949 63.04
0.69231 62.71
0.70513 62.39
0.71795 62.07
0.73077 61.77
0.74359 61.47
0.75641 61.18
0.76923 60.89
0.78205 60.6
0.79487 60.32
0.80769 60.05
0.82051 59.8
0.83333 59.52
0.84615 59.56
0.85897 59.21
0.87179 58.76
0.88462 58.51
0.89744 58.26
0.91026 58.02
0.92307 57.59
0.935897 57.56
0.94872 57.34
0.96154 57.11
0.97436 56.89
0.98718 56.67
1 56.46
答案1
你可以x filter
像这样使用 a 将 x 乘以 13
\begin{filecontents}{A.dat}
Time Temperature
0 100
0.00128 98.50
0.00256 97.89
0.00641 96.82
0.01282 95.54
0.97436 45.1
0.98718 44.78
1 44.48
\end{filecontents}
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlargelimits=false,
ymin=30, ymax=110,
xmin=0, xmax=13,
ytick={30,40,...,110},
ylabel={Temperature [°$\mathrm{C}$]},
xlabel={Time [h]},
ymajorgrids=true,
grid style=dashed,
scaled ticks=false,
x tick label style={rotate=90},
x filter/.expression={13*x},
]
\addplot+[
mark=triangle,
mark size=1pt]
table[meta=Temperature]{A.dat};
\addlegendentry{A}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
替代x filter
所示hpekristiansen 的回答你可以使用x expr
。有关详细信息,请查看代码。
% used PGFPlots v1.18.1
\begin{filecontents}{A.dat}
Time Temperature
0 100
0.00128 98.50
0.00256 97.89
0.00641 96.82
0.01282 95.54
0.97436 45.1
0.98718 44.78
1 44.48
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=30, ymax=110,
xmin=0, xmax=13,
ytick distance=10, % (<-- changed)
ylabel={Temperature [°C]},
xlabel={Time [h]},
]
\addplot+[
mark=triangle,
mark size=1pt,
] table [x expr={13*\thisrow{Time}}] {A.dat};
\addlegendentry{A}
\end{axis}
\end{tikzpicture}
\end{document}