我有这个 MWE。我的问题是如何在轴上设置 xsep 和 ystep,以便它们正确显示,而无需事先知道值的范围:xtick={0,0.1,...,1}
,ytick={80,85,...,120}
。例如,对于x
,我希望只有类似的东西xstep=0.1
。
\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.8]{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.dat}
x1 y1
86.173 0
92.9831 0.052631
93.9947 0.105263
94.6829 0.157895
95.2628 0.210526
95.8316 0.263158
96.3317 0.315789
96.7983 0.368421
97.2488 0.421053
97.6326 0.473684
98.0416 0.526316
98.4448 0.578947
98.9012 0.631579
99.3715 0.684211
99.908 0.736842
100.46 0.789474
101.202 0.842105
102.183 0.894737
103.592 0.947368
113.909 1
\end{filecontents*}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\usetikzlibrary{plotmarks}
\pgfplotsset{%
compat=newest,
compat/show suggested version=false,
xmin=80,xmax=120,
ymin=0,ymax=1,
xtick={0,0.1,...,1},
ytick={80,85,...,120},
legend style={%
legend pos=south east,
legend cell align=left,
},
}
\begin{filecontents*}{\jobname.tikz}
\begin{tikzpicture}
\selectcolormodel{gray}
\begin{axis}
\addplot[mark=square] table[x=x1, y=y1] {\jobname.dat};
\legend{Testing}
\end{axis}
\end{tikzpicture}
\end{filecontents*}
\begin{document}
\includegraphics{\jobname.tikz}
\end{document}
答案1
您仍然可以强行按照评论中给出的想法行事。但您的工作流程非常有趣,尽管我不知道您为什么要这样做。compat=newest
如果将来的版本决定更改您使用的选项,也会破坏您的代码。
在这里,我们将最小值和最大值声明嵌入到自定义样式中,并在样式中临时进行少量计算。这可能有点过头了,但可以将其作为概念证明。我仍然认为手动方法会更容易,但这只是我的观点。
\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.8]{helvet}
\usepackage{pgfplots}
\pgfplotstableread{
x1 y1
86.173 0
92.9831 0.052631
93.9947 0.105263
94.6829 0.157895
95.2628 0.210526
95.8316 0.263158
96.3317 0.315789
96.7983 0.368421
97.2488 0.421053
97.6326 0.473684
98.0416 0.526316
98.4448 0.578947
98.9012 0.631579
99.3715 0.684211
99.908 0.736842
100.46 0.789474
101.202 0.842105
102.183 0.894737
103.592 0.947368
113.909 1
}\mytable
\pgfplotsset{mytick x interval/.style args={#1:#2:#3}{
xmin=#1,xmax=#3,myincrement={#1}{#2},
xtick={#1,\myval,...,#3}
},
myincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\myval{\pgfmathresult}}
}
\pgfplotsset{%
compat=1.8,
ymin=0,ymax=1,
legend style={%
legend pos=south east,
legend cell align=left,
},
mytick x interval=80:5:120,
}
\begin{document}
\begin{tikzpicture}
\selectcolormodel{gray}
\begin{axis}
\addplot[mark=square] table[x=x1, y=y1] {\mytable};
\legend{Testing}
\end{axis}
\end{tikzpicture}
\end{document}