我想问一下如何将 y 标签放在左侧垂直线上方而不是 0 上方。另外请向我解释一下你答案中代码的含义。
图片:
我的代码:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}[
no markers, domain=-4:4, samples=100,
axis lines*=left, xlabel=$x$, ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west},
height=5cm, width=10cm,
xtick={0}, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major]
\addplot [fill=cyan!20, draw=none, domain=-4:2] {gauss(0,1)} \closedcycle;
\addplot [very thick,cyan!50!black] {gauss(0,1)};
\draw [yshift=-0.6cm, latex-latex](axis cs:2,0) -- node [fill=white] {\small $z=2$} (axis cs:0,0);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
将样式更改every axis y label
为
every axis y label/.style={at={(rel axis cs:0,1)},anchor=south},
坐标rel axis cs
系的左下角为(0,0),右上角为(1,1),所以(0,1)就是右上角。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}[
no markers, domain=-4:4, samples=100,
axis lines*=left, xlabel=$x$, ylabel=$y$,
every axis y label/.style={at={(rel axis cs:0,1)},anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west},
height=5cm, width=10cm,
xtick={0}, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major]
\addplot [fill=cyan!20, draw=none, domain=-4:2] {gauss(0,1)} \closedcycle;
\addplot [very thick,cyan!50!black] {gauss(0,1)};
\draw [yshift=-0.6cm, latex-latex](axis cs:2,0) -- node [fill=white] {\small $z=2$} (axis cs:0,0);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
或简单every axis y label/.style={at={(0,1)},anchor=south},
:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}[
no markers, domain=-4:4, samples=100,
%
axis lines=left,
xlabel=$x$, ylabel=$y$,
every axis y label/.style={at={(0,1)},anchor=south},
every axis x label/.style={at={(1,0)},anchor=west},
height=5cm, width=10cm,
xtick={0}, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major]
\addplot [fill=cyan!20, draw=none, domain=-4:2] {gauss(0,1)} \closedcycle;
\addplot [very thick,cyan!50!black] {gauss(0,1)};
\draw [yshift=-0.6cm, latex-latex](axis cs:2,0) -- node [fill=white] {\small $z=2$} (axis cs:0,0);
\end{axis}
\end{tikzpicture}
\end{document}
答案3
您可以为此使用不同的坐标系,例如rel axis cs
或axis description cs
。但在我看来,最好的方法是为相应的轴使用正确的坐标系:xticklabel* cs
和yticklabel* cs
(如果您不使用星号,也会考虑刻度标签,这可能很方便。这里不需要)
这是修改后的代码:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{
compat = 1.14 % n=most recent feature set, not necessarily needed
}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}
[
no markers,
domain=-4:4,
samples=100,
axis x line=bottom, % place axis explicitly
axis y line=left,
xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at={(yticklabel* cs: 1)}, anchor = south}, % change here
every axis x label/.style={at={(xticklabel* cs: 1)}, anchor = west}, % change here
height=5cm, width=10cm,
xtick={0}, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major
]
\addplot [fill=cyan!20, draw=none, domain=-4:2] {gauss(0,1)} \closedcycle;
\addplot [very thick,cyan!50!black] {gauss(0,1)};
\draw [yshift=-0.6cm, latex-latex](axis cs:2,0) -- node [fill=white] {\small $z=2$} (axis cs:0,0);
\end{axis}
\end{tikzpicture}
\end{document}