我正在尝试绘制下面的图,但我想知道如何旋转高斯函数,就像在第一个图中一样?
\documentclass[10pt, compress, usetitleprogressbar]{beamer}
%Tikz for drawing
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.multipart}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\pgfmathdeclarefunction{cline}{2}{%
\pgfmathparse{#1*x+#2}%
}
\begin{tikzpicture}[auto, scale=0.58, every
node/.style={transform shape}]
\begin{axis}[very thick,cyan!30!black,
no markers, domain=0:6, samples=100,
axis lines*=left, xlabel=Measured Value (x), ylabel=Probability Density,
height=5cm, width=7cm,
xtick=\empty, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major, name=plot1, ymax=1
]
\addplot [very thick,cyan!50!black, fill=cyan!10, rotate
around={-90:(9,9)},] {gauss(3,0.5)};
\end{axis}
\begin{axis}[very thick,cyan!30!black,
no markers, domain=0:6, samples=100,
axis lines*=left, xlabel=Measured Value (x), ylabel=Probability Density,
height=5cm, width=7cm,
xtick=\empty, ytick=\empty,
%xtick={-3, -2, -1, 0, 1, 2, 3}, ytick=empty,
enlargelimits=false, clip=false, axis on top,
grid = major, name=plot2,
at=(plot1.right of south east), anchor=left of south west,
ymax=1
]
\addplot [very thick,cyan!50!black, fill=cyan!10] {cline(1,0.5)};
\end{axis}
\begin{axis}[very thick,cyan!30!black,
no markers, domain=0:6, samples=100,
axis lines*=left, xlabel=Measured Value (x), ylabel=Probability Density,
height=5cm, width=7cm,
xtick=\empty, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major, name=plot3,
at=(plot2.below south west), anchor=above north west,
ymax=1]
]
\addplot [very thick,cyan!50!black, fill=cyan!10] {gauss(3,0.5)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
尝试
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{positioning}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((\x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\begin{tikzpicture}[
node distance = 4mm,
font = \small\sffamily,
N/.style = {name=n#1,
shape=rectangle,fill=gray!10,
minimum size=44mm,
node contents={}},
domain = 0:4,
samples = 20,
]
\node[N=1,above right];
\draw[<-] (n1.south west) --
node[left] {\rotatebox{90}{calculated value $(y)$}}
(n1.north west);
\draw[->] (n1.north west) --
node[above] {probability density}
(n1.north east);
\draw[draw=cyan!50!black, fill=cyan!10, very thick, smooth,
% transform canvas={yshift=4mm}
]
plot ({gauss(2,0.4)},\x);
%---
\node[N=2,right=of n1];
\draw[->] (n2.south west) -- (n2.north west) node[above] {$(y)$};
\draw[->] (n2.south west) -- (n2.south east) node[right] {$(x)$};
\draw[very thick, shorten <=2mm]
(n2.south west) -- node[pos=0.75,below,sloped] {$y(x)=a+bx$} (n2.north east) ;
%---
\node[N=3,below=of n2];
\draw[->] (n3.south west) --
node[left] {\rotatebox{90}{measured value $(y)$}}
(n3.north west);
\draw[->] (n3.south west) --
node[below] {probability density}
(n3.south east);
\draw[draw=cyan!50!black, fill=cyan!10, very thick, smooth,
transform canvas={xshift=48mm, yshift=-48mm}
]
plot (\x,{gauss(2,0.4)});
%---
\draw[dashed] ([yshift=-2mm] n1.west) -| ([xshift=-2mm] n3.south);
\end{tikzpicture}
\end{document}
在您的(不工作的)MWE中,我进行了以下更改:
beamer
而不是我使用的文档类standalone
- 我省略了未使用的 TikZ 库和函数定义
cline
- 我改变了 Gaus pdf 的参数
- 使用 John Kormylov 的建议,将所有元素绘制在三个节点中(以
- 很容易看到它们,它们是灰色的;在最终版本中你可以省略这个填充)适应图片大小到投影仪框架的大小
对于第一个节点中的高斯pdf,我简单地改变了坐标,因此该函数不需要旋转。
答案2
你可以很好地做到这一点元帖子也一样。
一旦创建了正常形状的曲线,您就可以根据需要缩放和重新定位它。
prologues := 3;
outputtemplate := "%j%c.eps";
beginfig(0);
% unit
u := 1cm;
% axes
path xx, yy;
xx = (origin -- 5 right) scaled u;
yy = xx rotated 90;
drawarrow xx; label.rt (btex $x$ etex, point 1 of xx);
drawarrow yy; label.top(btex $y$ etex, point 1 of yy);
% parameters, assuming y=a+bx
a = 0.6u;
b = 0.7;
x_mean = 2.7u;
% the regression line, and the normal curves
path lm, Z, Zx, Zy;
lm = (0,a) -- (5u,a+b*5u);
% the regression mean point
z0 = (x_mean, a+b*x_mean);
% scale is arbitrary, so you only need the core of the probability distribution,
% and so the "3" is just to make it look nice
Z = ((-4,0) for x = -3.9 step .1 until 4: .. (x, 3 mexp(-128*(x**2))) endfor) scaled 1/2 u;
margin = -2u;
% for the x-axis you just want a copy of Z shifted down and across
Zx = Z shifted (x0,margin);
% for the y-axis you can make it more realistic if you scale it by the regression coefficient
% before you rotate it and shift it
Zy = Z xscaled b rotated -90 shifted (margin,y0);
% put in the lines connecting the measurements to the response
alpha = 1.96 * 1/2 u;
for i=-1 upto 1:
draw (x0+(i*alpha),margin) -- (x0+(i*alpha),y0+(i*b*alpha)) -- (margin,y0+(i*b*alpha))
cutbefore Zx cutafter Zy
dashed if i=0: evenly else: withdots scaled 0.6 fi;
endfor
% draw the lm line and the normal curves and then label them
draw lm withcolor .67 red;
draw Zx;
draw Zy;
label.bot(btex Measured value ($x$) etex, (x0,margin));
label.lft(btex Response value ($y$) etex rotated 90, (margin,y0));
label.top(btex $y=a+bx$ etex, point 1 of lm);
endfig;
end