我正在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}
我在這裡想要的是在保留 all 的同時\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}