它基本上是关于两个图,一个在上面,一个在下面。上面的图实际上是一个动画,会自动播放。下面的图是一个静态图。水平轴是所有图的共同点,这就是为什么我想将它们水平对齐,这样 x 轴在所有图中都有相同的水平跨度。
在继续之前,一张“图”胜过千言万语:
\documentclass{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{animate}
\begin{document}
\begin{frame}
\pgfmathsetmacro{\PI}{3.141592654}
\begin{animateinline}[palindrome, autoplay,
begin={\begin{tikzpicture}%[show background rectangle]
\pgfplotsset{yticklabel style={text width=1.3em,align=right},
xticklabels={,,},
width=0.8\linewidth, height=0.3\textheight,
/tikz/font=\scriptsize, ylabel={$[\text{dB}]$}
}
},
end={\end{tikzpicture}}
]{2}
\begin{axis}[name=plot1, ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.9)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.8)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.9)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.8)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.9)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.8)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.9)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.8)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.9)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.8)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.9)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.8)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.9)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.8)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.9)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.8)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.9)};
\addplot [brown!30] coordinates { (.1,.8)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\end{animateinline}
\vspace{-0.9ex}
\begin{tikzpicture}%[show background rectangle]
\pgfplotsset{every axis x label/.style={at={(ticklabel* cs:1.10)}, yshift = -6.5pt, anchor=west,},
/tikz/font = \scriptsize,
width = 0.654\linewidth, height = .4\textheight,
}
\begin{axis}[name=plot2, at=(plot1.south), anchor=center,
yticklabel style={text width=1.5em,align=left},
axis y line* = right,
scale only axis,
scaled y ticks={real:\PI},
ylabel = {\textcolor{gray!50}{Kippwinkel}},
ytick scale label code/.code={},
ytick distance=\PI/2,
yticklabel={\pgfmathparse{round(10*\tick)/10}
\pgfmathifisint{\pgfmathresult}{\strut$\pgfmathprintnumber[int detect]{\pgfmathresult}\pi$%
}{\strut$\pgfmathprintnumber{\pgfmathresult}\pi$}
},
yticklabel style={/pgf/number format/frac,/pgf/number format/frac whole=false, color = gray!50},
xmin=0, xmax=1,
xlabel = Z-Pixels,
xlabel style = {yshift=-2.5ex},
xticklabel style={yshift=-2.5ex},
xticklabel={\pgfmathparse{round(384*\tick)}\strut$\pgfmathprintnumber{\pgfmathresult}$},
]
\addplot[color=gray!50,smooth] coordinates {(.1,-3)(.2,-1)(.3,1)(.4,3)(.5,1)(.6,-1)(.7,-3)(.8,-1)(.9,1)};
\end{axis}
\begin{axis}[at=(plot2), anchor=center,
yticklabel style={text width=1.3em,align=right},
axis y line* = left,
scale only axis,
xmin=0, xmax=1,
xlabel = {Tiefe $[\text{mm}]$},
ylabel = {\textcolor{red}{Breite $[\mu\text{m}]$}},
yticklabel style={color = red},
]
\addplot[color=red,smooth,thick] coordinates {(.1,40)(.2,-35)(.3,30)(.4,-25)(.5,20)(.6,-15)(.7,10)(.8,-5)(.9,0)};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
为了对齐我正在绘制的图\pgfplotsset{width=...}
(遗憾的是并非所有图的宽度都相同)和yticklabel style={text width=1.5em,align=left}
指令。但我不喜欢这种方法。它不仅不够优雅。而且很难实现自动化。在现实世界中,坐标是由收集测量值并计算模型的程序生成的。程序运行后,脚本会编译 tex 文件以生成某种报告。但脚本并不总是能得出正确的距离来填写指令。
有没有更强大的方法来实现水平对齐?
提前非常感谢您!
答案1
正如前面提到的问题下方的评论您只需添加scale only axis
到“所有”axis
环境并赋予相同的width
值即可。
% used PGFPlots v1.14
\documentclass{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepackage{animate}
\begin{document}
\begin{frame}
\pgfmathsetmacro{\PI}{3.141592654}
\begin{animateinline}[
palindrome,
autoplay,
begin={%
\begin{tikzpicture}
\pgfplotsset{
yticklabel style={text width=1.3em,align=right},
% changed to `\empty'
xticklabels=\empty,
% ---------------------------------------------------------
% added `scale only axis' ...
scale only axis,
% ... and set to the same `width' as below
width = 0.65\linewidth,
% ---------------------------------------------------------
height=0.3\textheight,
/tikz/font=\scriptsize,
ylabel={$[\text{dB}]$},
}
},
end={\end{tikzpicture}}
]{2}
\begin{axis}[name=plot1, ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.9)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.8)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.9)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.8)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.9)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.8)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.9)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.8)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.9)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.8)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.9)(.7,.0)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.8)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.9)(.8,.0)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.0)(.3,.8)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.9)(.9,.0)};
\addplot [brown!30] coordinates { (.1,.0)(.2,.8)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\newframe
\begin{axis}[ymin=-.2, ymax=1.1, xmin=0, xmax=1]
\addplot [green!30] coordinates { (.1,.0)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.9)};
\addplot [brown!30] coordinates { (.1,.8)(.2,.0)(.3,.0)(.4,.0)(.5,.0)(.6,.0)(.7,.0)(.8,.0)(.9,.0)};
\end{axis}
\end{animateinline}
\vspace{-0.9ex}
\begin{tikzpicture}
\pgfplotsset{
every axis x label/.style={
at={(ticklabel* cs:1.10)},
yshift = -6.5pt,
anchor=west,
},
/tikz/font = \scriptsize,
width = 0.65\linewidth,
height = .4\textheight,
% -----------------------------------------------------------------
% added `scale only axis'
scale only axis,
% -----------------------------------------------------------------
}
\begin{axis}[
name=plot2,
at=(plot1.south),
anchor=center,
yticklabel style={text width=1.5em,align=left},
axis y line* = right,
scaled y ticks={real:\PI},
ylabel = {\textcolor{gray!50}{Kippwinkel}},
ytick scale label code/.code={},
ytick distance=\PI/2,
yticklabel={%
\pgfmathparse{round(10*\tick)/10}%
\pgfmathifisint{\pgfmathresult}{%
\strut$\pgfmathprintnumber[int detect]{\pgfmathresult}\pi$%
}{%
\strut$\pgfmathprintnumber{\pgfmathresult}\pi$%
}
},
yticklabel style={
/pgf/number format/frac,
/pgf/number format/frac whole=false,
color = gray!50,
},
xmin=0,
xmax=1,
xlabel = Z-Pixels,
xlabel style = {yshift=-2.5ex},
xticklabel style={yshift=-2.5ex},
xticklabel={%
\pgfmathparse{round(384*\tick)}\strut$\pgfmathprintnumber{\pgfmathresult}$%
},
]
\addplot[color=gray!50,smooth] coordinates {(.1,-3)(.2,-1)(.3,1)(.4,3)(.5,1)(.6,-1)(.7,-3)(.8,-1)(.9,1)};
\end{axis}
\begin{axis}[
at=(plot2),
anchor=center,
yticklabel style={text width=1.3em,align=right},
axis y line* = left,
xmin=0, xmax=1,
xlabel = {Tiefe $[\text{mm}]$},
ylabel = {\textcolor{red}{Breite $[\mu\text{m}]$}},
yticklabel style={color = red},
]
\addplot[color=red,smooth,thick] coordinates {(.1,40)(.2,-35)(.3,30)(.4,-25)(.5,20)(.6,-15)(.7,10)(.8,-5)(.9,0)};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}