我正在pgf
动态生成图表,我想避免y ticks
在我的图表中添加非整数,同时保留所有的x ticks
。这是一个 MWE:
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{amssymb}
\usepackage{pgfplotstable}
\usepackage[active, pdftex, tightpage]{preview}
\PreviewEnvironment[{[]}]{tikzpicture}
\setlength\PreviewBorder{2mm}
\begin{filecontents}{PJxpGrZtwos.tex.dat}
N f M
1024 3.407938e+01 25
1536 1.339487e+01 20
2048 1.139487e+01 19
3072 8.351236e+00 14
4049 6.466788e+00 13
6144 4.466788e+00 11
\end{filecontents}
\pgfkeys{/pgf/fpu=true}
\pgfplotstableread{PJxpGrZtwos.tex.dat}\loadedtable
\pgfplotstablegetelem{0}{N}\of{\loadedtable}
\let\relativeTo\pgfplotsretval
\pgfplotstablegetelem{0}{f}\of{\loadedtable}
\let\f\pgfplotsretval
\pgfplotstablecreatecol[create col/expr={\f / \thisrow{f}}]{f-relative}\loadedtable
\pgfkeys{/pgf/fpu=false}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[axis y line*=left, legend pos=south east,xlabel={N},xtick=data, xticklabels from table={\loadedtable}{N},ytick=data, yticklabel=\pgfmathparse{exp(\tick)}\pgfmathprintnumber{\pgfmathresult}, ylabel ={$\dfrac{f(\pgfmathprintnumber\relativeTo)}{f(N)}$}]
\addplot[black] table[x=N, y expr=\thisrow{N}/\relativeTo] {\loadedtable};
\addplot[blue, mark=*] table[x=N, y=f-relative] {\loadedtable};
\legend{{Linear progression},}
\end{loglogaxis}
\begin{semilogxaxis}[ymin = 5, ytick={10, 15, ..., 30}, ymax = 35, axis x line=none,ylabel={\# M}, axis x line=none, axis y line*=right, ylabel near ticks=right, yticklabel pos=right]
\addplot[red, dotted, mark=x, mark options={solid}] table[x=N, y=M] {\loadedtable};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
我在这里想要的是不要\addplot[black] table[x=N, y expr=\thisrow{N}/\relativeTo] {\loadedtable};
添加y ticks
非整数值,\thisrow{N}/\relativeTo
同时保留所有x ticks
。
如果您有其他方法可以绘制从 (x_min, 1) 到 (x_max, x_max/x_min) 的线性函数,同时具有良好的对应性y ticks
(也许通过创建另一个表?),那么这也可以解决问题。
请记住,这是用于“即时”情节生成,我不知道我的先验价值,所以在这种情况下我无法使用y tick={1,...,6}
。
答案1
您可以通过一些 if 条件为刻度标签引入一些容忍度。
\documentclass{standalone}
\usepackage{amsmath,amssymb,siunitx}
\usepackage{pgfplotstable}
\begin{filecontents}{PJxpGrZtwos.tex.dat}
N f M
1024 3.407938e+01 25
1536 1.339487e+01 20
2048 1.139487e+01 19
3072 8.351236e+00 14
4049 6.466788e+00 13
6144 4.466788e+00 11
\end{filecontents}
\pgfkeys{/pgf/fpu=true}
\pgfplotstableread{PJxpGrZtwos.tex.dat}\loadedtable
\pgfplotstablegetelem{0}{N}\of{\loadedtable}
\let\relativeTo\pgfplotsretval
\pgfplotstablegetelem{0}{f}\of{\loadedtable}
\let\f\pgfplotsretval
\pgfplotstablecreatecol[create col/expr={\f / \thisrow{f}}]{f-relative}\loadedtable
\pgfkeys{/pgf/fpu=false}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
axis y line*=left,
legend pos=south east,
xlabel={N},
xtick=data,
xticklabels from table={\loadedtable}{N},
ytick=data,
yticklabel={
\pgfmathparse{abs(exp(\tick) - int(exp(\tick)))}
\ifdim\pgfmathresult pt>0.999 pt
\pgfmathparse{exp(\tick)}
\pgfmathprintnumber{\pgfmathresult}
\else
\ifdim\pgfmathresult pt<0.01 pt
\pgfmathparse{exp(\tick)}
\pgfmathprintnumber{\pgfmathresult}
\else{}
\fi
\fi
},
ylabel ={$\dfrac{f(\pgfmathprintnumber\relativeTo)}{f(N)}$}]
\addplot[black] table[x=N, y expr=\thisrow{N}/\relativeTo] {\loadedtable};
\addplot[blue, mark=*] table[x=N, y=f-relative] {\loadedtable};
\legend{{Linear progression},}
\end{loglogaxis}
\begin{semilogxaxis}[ymin = 5, ytick={10, 15, ..., 30}, ymax = 35, axis x line=none,ylabel={\# M}, axis x line=none, axis y line*=right, ylabel near ticks=right, yticklabel pos=right]
\addplot[red, dotted, mark=x, mark options={solid}] table[x=N, y=M] {\loadedtable};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
答案2
对我来说,显而易见的解决方案是删除“ytick=data”部分。我误解了什么吗?
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage[active, pdftex, tightpage]{preview}
\PreviewEnvironment[{[]}]{tikzpicture}
\setlength\PreviewBorder{2mm}
\begin{filecontents}{Dkb0rjC9L.tex.dat}
x y
1 1
2 1.5
3 2
4 3
5 4
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xtick=data]
\addplot table {Dkb0rjC9L.tex.dat};
\end{axis}
\end{tikzpicture}
\end{document}