为什么线条超出了横坐标轴(蓝色圆圈)?
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{amssymb}
\begin{document}
\pgfmathdeclarefunction{gauss}{1}{%
\pgfmathparse{1/(sqrt(2*pi))*exp(-((#1)^2)/2)}%
}
\begin{tikzpicture}
\begin{axis}[
domain=-3:3,
tickwidth=0.1cm,
major tick style=black,
axis lines=middle,
axis line style =thick,
axis line style={shorten >=-10pt, shorten <=-15pt},
xlabel=$x$,ylabel=$y$,
xtick={-2,...,2},ytick={0,0.1,0.2,0.3},
xticklabels={,,,1,,,},
yticklabels={,0.1,,},
height=5cm, width=10cm,
clip=false,
axis on top,
every axis x label/.style={
at={(ticklabel* cs:1.03)},
anchor=west},
every axis y label/.style={
at={(ticklabel* cs:1.1)},
anchor=south},
samples=1000,
>=stealth,
]
\pgfmathsetmacro\valueA{gauss(0.8)}
\pgfmathsetmacro\valueB{gauss(-1.5)}
\addplot [fill=orange!40, draw=none, domain=-1.5:0.8] {gauss(x)} \closedcycle;
\draw [orange!80,thick] (axis cs:0.8,0) -- (axis cs:0.8,\valueA);
\draw [orange!80,thick] (axis cs:-1.5,0) -- (axis cs:-1.5,\valueB);
\addplot [ thick] {gauss(x)};
\node[below,orange!80] at (axis cs:0.8,0){$b$};
\node[below,orange!80] at (axis cs:-1.5,0){$a$};
\node[below left] at (axis cs:0,0){$0$};
\draw[orange,->](axis cs:2,0.4)--(axis cs:0.2,0.25);
\draw[blue] (axis cs:0.8,0) circle [radius=0.2cm];
\draw[blue] (axis cs:-1.5,0) circle [radius=0.2cm];
\node[draw][orange,anchor=south west] at (axis cs:2,0.4) {$P(a\leqslant X \leqslant b)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您的问题是您的x
轴没有经过原点(0,0)
要修复,只需设置ymin
为0
或更少。
输出
代码
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{amssymb}
\begin{document}
\pgfmathdeclarefunction{gauss}{1}
{%
\pgfmathparse{1/(sqrt(2*pi))*exp(-((#1)^2)/2)}%
}
\begin{tikzpicture}
\begin{axis}
[
ymin=0, %%%%%%%%%%%%%%%%%%%%%%%%%%% <--- just do this
domain=-3:3,
tickwidth=0.1cm,
major tick style=black,
axis lines=middle,
axis line style =thick,
axis line style={shorten >=-10pt, shorten <=-15pt},
xlabel=$x$,ylabel=$y$,
xtick={-2,...,2},ytick={0,0.1,0.2,0.3},
xticklabels={,,,1,,,},
yticklabels={,0.1,,},
height=5cm, width=10cm,
clip=false,
axis on top,
every axis x label/.style=
{
at={(ticklabel* cs:1.03)},
anchor=west
},
every axis y label/.style=
{
at={(ticklabel* cs:1.1)},
anchor=south
},
samples=100,
>=stealth,
]
\pgfmathsetmacro\valueA{gauss(0.8)}
\pgfmathsetmacro\valueB{gauss(-1.5)}
\addplot [fill=orange!40, draw=none, domain=-1.5:0.8] {gauss(x)} \closedcycle;
\draw [orange!80,thick] (axis cs:0.8,0) -- (axis cs:0.8,\valueA);
\draw [orange!80,thick] (axis cs:-1.5,0) -- (axis cs:-1.5,\valueB);
\addplot [ thick] {gauss(x)};
\node[below,orange!80] at (axis cs:0.8,0){$b$};
\node[below,orange!80] at (axis cs:-1.5,0){$a$};
\node[below left] at (axis cs:0,0){$0$};
\draw[orange,->](axis cs:2,0.4)--(axis cs:0.2,0.25);
\draw[blue] (axis cs:0.8,0) circle [radius=0.2cm];
\draw[blue] (axis cs:-1.5,0) circle [radius=0.2cm];
\node[draw][orange,anchor=south west] at (axis cs:2,0.4) {$P(a\leqslant X \leqslant b)$};
\end{axis}
\end{tikzpicture}
\end{document}
干杯,