我有下图,我想注释并添加一些箭头,但线条和文本有点旋转混乱。我在下面附上了我正在使用的 tikz 代码和一张画有线条和标签的图像。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\pgfmathsetmacro{\offset}{0.05}
\begin{tikzpicture}[font=\sffamily\small,
declare function={gauss(\x,\y,\z)=\offset+1.2/(\y*sqrt(2*pi))*exp(-((\x- \z)^2)/(2*\y^2));}]
\begin{axis}[samples=101,smooth,hide axis,width=20cm,height=8cm]
\addplot [domain=-3:3] ({gauss(x,1,0)},x);
\addplot [domain=-3:3] ({1+gauss(x,1,0)},x);
\draw[-latex] (0,-3) -- (0,3) coordinate[pos=0.35](x1) coordinate[pos=0.5] (y1) node[below right,rotate=-90]{$X_k$};
\draw[-latex] (1,-3) -- (1,3) coordinate[pos=0.60](x2) coordinate[pos=0.5](y2) node[below right,rotate=-90]{$X_1$};
\addplot[-latex] coordinates{(-0.5,-4) (2,-4)};
\path (0,-4) coordinate (z1) (1,-4) coordinate (z2) (2,-4) coordinate (z3);
\coordinate (t0) at (-0.45,-4.1);
\coordinate (t) at (2,-4.1);
\coordinate (xi) at (-0.5,4);
\addplot[-latex] coordinates{(-0.5,-4.2) (-0.5,4)}; % yaxis
\end{axis}
\foreach \X in {1,2}
{
\draw ([xshift=-1mm]y\X) -- ([xshift=1mm]y\X);
\draw ([yshift=1mm]z\X) -- ([yshift=-1mm]z\X) node[below] {$t_\X$};
}
\node[anchor=north east] at (t) {time};
\node[anchor=north east] at (xi) {$X$};
\end{tikzpicture}
\end{document}
答案1
您可以在里面添加线条axis
并使用这些坐标。我还添加了clips=false
以避免在轴下方绘制提示箭头的问题。
\documentclass[tikz,border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\pgfmathsetmacro{\offset}{0.05}
\begin{tikzpicture}[font=\sffamily\small,
declare function={gauss(\x,\y,\z)=\offset+1.2/(\y*sqrt(2*pi))*exp(-((\x- \z)^2)/(2*\y^2));}]
\begin{axis}[samples=101,smooth,hide axis,width=20cm,height=8cm, clip=false]
\addplot [domain=-3:3] ({gauss(x,1,0)},x);
\addplot [domain=-3:3] ({1+gauss(x,1,0)},x);
\draw[-latex] (0,-3) -- (0,3) coordinate[pos=0.35](x1) coordinate[pos=0.5] (y1) node[below right,rotate=-90]{$X_k$};
\draw[-latex] (1,-3) -- (1,3) coordinate[pos=0.60](x2) coordinate[pos=0.5](y2) node[below right,rotate=-90]{$X_1$};
\addplot[-latex] coordinates{(-0.5,-4) (2,-4)};
\path (0,-4) coordinate (z1) (1,-4) coordinate (z2) (2,-4) coordinate (z3);
\coordinate (t0) at (-0.45,-4.1);
\coordinate (t) at (2,-4.1);
\coordinate (xi) at (-0.5,4);
\addplot[-latex] coordinates{(-0.5,-4.2) (-0.5,4)}; % yaxis
\draw[->, red] (-0.5,-5)--(0,-5) node[above,midway]{A};
\draw[->,red] (0,-1) node[left]{B}--({1+gauss(-1,1,0)},-1)node[below right]{C};
\end{axis}
\foreach \X in {1,2}
{
\draw ([xshift=-1mm]y\X) -- ([xshift=1mm]y\X);
\draw ([yshift=1mm]z\X) -- ([yshift=-1mm]z\X) node[below] {$t_\X$};
}
\node[anchor=north east] at (t) {time};
\node[anchor=north east] at (xi) {$X$};
\end{tikzpicture}
\end{document}
答案2
您拥有高斯的参数化,因此很容易将点B
和定义C
为某些输入参数的函数,我\xp
在这里将其称为。然后您已经将坐标导出轴外,您也可以对标有的箭头执行此操作A
:只需将其绘制在轴外即可。然后无需关闭clip
。(在当前设置中,您可以关闭clip
,因为Ignasi 演示但是,如果您要在图中添加更多成分,这可能不是最佳方法,因为通常最好让 pgfplots 剪掉辅助内容。)
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\pgfmathsetmacro{\offset}{0.05}
\pgfmathsetmacro{\xp}{-1}
\begin{tikzpicture}[font=\sffamily\small,
declare function={gauss(\x,\y,\z)=\offset+1.2/(\y*sqrt(2*pi))*exp(-((\x- \z)^2)/(2*\y^2));}]
\begin{axis}[samples=101,smooth,hide axis,width=20cm,height=8cm]
\addplot [domain=-3:3] ({gauss(x,1,0)},x);
\addplot [domain=-3:3] ({1+gauss(x,1,0)},x);
\draw[-latex] (0,-3) -- (0,3) coordinate[pos=0.35](x1) coordinate[pos=0.5] (y1) node[below right,rotate=-90]{$X_k$};
\draw[-latex] (1,-3) -- (1,3) coordinate[pos=0.60](x2) coordinate[pos=0.5](y2) node[below right,rotate=-90]{$X_1$};
\addplot[-latex] coordinates{(-0.5,-4) (2,-4)};
\path (0,-4) coordinate (z1) (1,-4) coordinate (z2) (2,-4) coordinate (z3)
(-0.5,-5) coordinate (h1) (0,-5) coordinate (h2);
\coordinate (t0) at (-0.45,-4.1);
\coordinate (t) at (2,-4.1);
\coordinate (xi) at (-0.5,4);
\addplot[-latex] coordinates{(-0.5,-4.2) (-0.5,4)}; % yaxis
\coordinate (start) at (0,\xp);
\coordinate (end) at ({1+gauss(\xp,1,0)},\xp);
\end{axis}
\foreach \X in {1,2}
{
\draw ([xshift=-1mm]y\X) -- ([xshift=1mm]y\X);
\draw ([yshift=1mm]z\X) -- ([yshift=-1mm]z\X) node[below] {$t_\X$};
}
\draw[red,-latex] (start) node[left]{B}
-- (end) node[below right]{C};
\draw[red,-latex] (h1) -- (h2) node[midway,above]{A};
\node[anchor=north east] at (t) {time};
\node[anchor=north east] at (xi) {$X$};
\end{tikzpicture}
\end{document}