我正在尝试绘制下面的图画。
这四张图都是密度概率函数;情况 1 和情况 2 中的分布是单峰的,情况 3 中的分布是正态的,情况 4 中的分布是双峰的。我希望曲线看起来像草图,如下图所示。
我不知道在 tikz 环境中绘制简单曲线的最佳方法。我尝试过使用该命令\draw plot [smooth cycle] coordinates {(x0,y0), ... };
,但绘制每条曲线非常耗时。
相反,我尝试从帖子中获得灵感来做一些事情如何使用 pgfplot 创建高斯曲线?- 仅适用于情况 3 中的曲线。我仍然无法获得正确的曲线宽度。
我的代码如下:
\begin{tikzpicture}
% bell curve
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
% origo
\node[circle,fill=black,inner sep=1pt] (origo) at (0,0) {};
% y axis
\draw[->] (origo) -- (0,6);
% horizontal lines
\draw[->] (-3,0) -- (3,0);
\draw[->] (-3,1.5) -- (3,1.5);
\draw[->] (-3,3) -- (3,3);
\draw[->] (-3,4.5) -- (3,4.5);
% node
\node[right,inner sep=4pt,outer sep=4pt] at (3,4.5) {\large{$x$}};
% case nodes
\node[left, inner sep=4pt,outer sep=4pt] at (-3,4.5) {\large{1}};
\node[left, inner sep=4pt,outer sep=4pt] at (-3,3) {\large{2}};
\node[left, inner sep=4pt,outer sep=4pt] at (-3,1.5) {\large{3}};
\node[left, inner sep=4pt,outer sep=4pt] at (-3,0) {\large{4}};
% curves
\begin{axis}[hide axis,every axis plot post/.append style={
mark=none,domain=-3:3,samples=50,smooth},xshift=-0.95cm,yshift=1.5cm,height=3cm]
\addplot {\gauss{0}{1.7}};
\end{axis}
\end{tikzpicture}
答案1
我认为您的问题在于您没有定义 的width
,axis
因此使用了默认宽度。看起来您的线条宽度为 6cm,因此请设置width=6cm
。我认为还有许多其他设置会很有用,下面是一个经过修改的axis
环境,其中包含一些注释:
\begin{axis}[
hide axis,
enlargelimits=false, % removes whitespace between plot and axis
clip=false, % avoids clipping of plot lines at the axis border
width=6cm,height=1.3cm, % set size of axis
scale only axis, % don't inlude any ticks, labels etc. in the size calculation
every axis plot post/.append style={
mark=none,
domain=-3:3,
samples=50,
smooth},
anchor=origin, % set the anchor of the axis to the origin
at={(0,1.5cm)} % define the position of the axis in the coordinate system of the tikzpicture
]
\addplot {\gauss{0}{1.7}};
\end{axis}
保留其余代码,输出为
完全不同,也许groupplot
环境在这里会很有用。在下面的代码中,我使用了伽马分布(定义借用自使用 tikz 包绘制分布) 来近似非中性情况。不过,这有点儿不妥。目前没有时间添加太多解释,如果需要的话可以稍后再添加。
我还添加了一个示例,说明如何仅使用贝塞尔曲线而不进行任何绘图即可进行草图绘制。
\documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}[
declare function={gamma(\z)=
2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);},
declare function={gauss(\x,\mu,\sig)=exp(-((\x-\mu)^2)/(2*\sig^2))/(\sig*sqrt(2*pi));}
]
\begin{groupplot}[
group style={
group size=1 by 4,
vertical sep=0pt,
group name=G},
xtick=\empty,ytick=\empty,
width=8cm,height=3cm,
axis lines=middle,
y axis line style={draw=none},
ymax=0.5,
clip=false,
no markers,
domain=0:8,
samples=100]
\nextgroupplot[
xlabel=$x$,
x label style={right,font=\large,at={(rel axis cs:0,0)}},
x axis line style={stealth-},
x post scale=-1]
\addplot {gammapdf(x,1.6,1.2)};
\nextgroupplot
\addplot {gammapdf(x,1.6,1.2)};
\nextgroupplot
\addplot+ [domain=-8:8] {gauss(x,0,1)};
\node [font=\tiny] at (0,0.1) {neutral};
\nextgroupplot[xmin=0,xmax=8]
\addplot+ [domain=0:4] {gammapdf(x,1.6,1.2)};
\end{groupplot}
\begin{axis}[width=8cm,height=3cm,ymax=0.3,at={(G c1r4.south west)},x post scale=-1,hide axis,xmin=0,xmax=8,ymax=0.5,enlargelimits=false,no markers]
\addplot+ [domain=0:4] {gammapdf(x,1.6,1.2)};
\end{axis}
\node [left,font=\large] at (G c1r1.south west) {$1$};
\node [left,font=\large] at (G c1r2.south west) {$2$};
\node [left,font=\large] at (G c1r3.south west) {$3$};
\node [left,font=\large] at (G c1r4.south west) {$4$};
\draw [-stealth] (G c1r4.south) -- ([yshift=0.3cm]G c1r1.north);
\fill (G c1r4.south) circle[radius=2pt];
\end{tikzpicture}
\begin{tikzpicture}[y=1.5cm]
\foreach [count=\i] \y in {4,3,2,1}
\draw (0,\y) node [left,font=\large]{$\i$} -- (6,\y );
\node [right] at (6,4) {$x$};
\draw (0,3.05) to[out=0,in=180] ++(1.2,0.7) .. controls +(1,0) and +(-4.5,0) .. ++(4.7,-0.65);
\draw (0,2.05) .. controls +(3,0) and +(-0.7,0) .. (3,2.65) .. controls +(0.7,0) and +(-3,0) .. (6,2.05);
\draw (0,1.05) .. controls +(.7,0) and +(-.7,0) .. (1.2,1.7)
.. controls +(0.5,0) and +(-1.7,0) .. (3,1.1)
.. controls +(1.7,0) and +(-0.5,0) .. (4.8,1.7)
.. controls +(0.7,0) and +(-.7,0) .. (6,1.05);
\draw (6,4.05) to[out=180,in=0] ++(-1.2,0.7) .. controls +(-1,0) and +(4.5,0) .. ++(-4.7,-0.65);
\fill (3,1) circle[radius=2pt];
\draw [-stealth] (3,1) -- (3,5);
\end{tikzpicture}
\end{document}