出于这个原因,我关注了这篇文章如何为与线性刻度 (x) 刻度不对齐的相同数据添加次级非线性缩放 x 轴 (1/x)。
我已经定义了自己的数组,我想用它来构建顶轴的 xticklabels:
\def\conc{{0, 0.5, 1, 5, 15.7, 24}}
但是这部分代码不起作用:
\pgfmathparse{\conc[\pgfmathresult]}
为什么会这样呢?
\documentclass[10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\def\conc{{0, 0.5, 1, 5, 15.7, 24}}
\pgfkeys{/pgfplots/.cd,master axis/.style={
scale only axis,
enlarge x limits=false,
axis x line*=bottom,
xticklabel shift=3pt,
after end axis/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathparse{\pgfkeysvalueof{/pgfplots/xmin}}
\global\let\masterxmin=\pgfmathresult
\pgfmathparse{\pgfkeysvalueof{/pgfplots/xmax}}
\global\let\masterxmax=\pgfmathresult
\pgfkeys{/pgf/fpu=false}
},
},
slave axis/.style={
scale only axis,enlarge x limits=false,
axis x line*=top,
axis y line=none,
xmin=\masterxmin,xmax=\masterxmax,
scaled x ticks=false,
xtick={0, 0.02862, 0.75184, 1},
xticklabel={%
\pgfkeys{/pgf/fpu}%
\pgfmathparse{\ticknum}
% \pgfmathparse{\conc[\pgfmathresult]}
% \pgfmathprintnumber{\pgfmathresult}
},
xticklabel style={/pgf/number format/.cd,fixed,precision=2},
xmajorgrids=true, % to show the tick position
every axis x grid/.style={red,dashed,very thick},
}
}
\begin{tikzpicture}
\begin{axis}[%
master axis,
xmin=0,
xmax=1.,
ylabel={G},
xlabel={f},
]
\addplot[no marks, draw, black,domain=0:1,samples=10] {x};
\end{axis}
\begin{axis}[%
slave axis,
xlabel={[HNO$_3$] (M)},
x label style={at={(axis description cs:.5,1.25)}},
]
\end{axis}
\end{tikzpicture}
\end{document}
答案1
正确的代码如下:
只有一件事应该改变:
从:
\pgfkeys{/pgf/fpu}
到:
\pgfkeys{/pgf/fpu=false}
\documentclass[10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\def\conc{{0, 0.5, 1, 5,10, 15.7, 24}}
\pgfkeys{/pgfplots/.cd,master axis/.style={
scale only axis,
enlarge x limits=false,
axis x line*=bottom,
xticklabel shift=3pt,
after end axis/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathparse{\pgfkeysvalueof{/pgfplots/xmin}}
\global\let\masterxmin=\pgfmathresult
\pgfmathparse{\pgfkeysvalueof{/pgfplots/xmax}}
\global\let\masterxmax=\pgfmathresult
\pgfkeys{/pgf/fpu=false}
},
},
slave axis/.style={
scale only axis,enlarge x limits=false,
axis x line*=top,
axis y line=none,
xmin=\masterxmin,xmax=\masterxmax,
scaled x ticks=false,
xtick={0, 0.02862, 0.75184, 1},
xticklabel={%
\pgfkeys{/pgf/fpu=false}%
\pgfmathparse{\ticknum}
\pgfmathparse{\conc[\pgfmathresult]}
\pgfmathprintnumber{\pgfmathresult}
\ticknum
},
xticklabel style={/pgf/number format/.cd,fixed,precision=2},
xmajorgrids=true, % to show the tick position
every axis x grid/.style={red,dashed,very thick},
}
}
\begin{tikzpicture}
\begin{axis}[%
master axis,
xmin=0,
xmax=1.,
ylabel={G},
xlabel={f},
]
\addplot[no marks, draw, black,domain=0:1,samples=10] {x};
\end{axis}
\begin{axis}[%
slave axis,
xlabel={[HNO$_3$] (M)},
x label style={at={(axis description cs:.5,1.25)}},
]
\end{axis}
\end{tikzpicture}
\end{document}