答案1
有几种方法/软件包可用于使用 LaTeX “编程”图表。也许目前最流行的软件包是 TikZ。使用 TikZ 也有多种方法可以做到这一点,我在下面介绍了一种选项,它会产生以下效果:
代码中有一些注释,但绝不是非常详细。手动的,乍一看可能很大而且令人生畏,但其实是一本非常有用的参考书。它还以一些教程开始,旨在向新用户介绍如何使用 TikZ 制作图表。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{
arrows.meta, % supplies the Stealth arrow tip
bending
}
\tikzset{
distribution/.pic={ % define a small "picture" that can be used multiple times
\draw [thick] (0,1) -- (0,-1);
\draw [thin,dotted] (0,0) coordinate (-origin) -- (0.7,0);
\draw [blue] (0.05,1) .. controls (0.08,0.2) and (0.6,0.3) ..
coordinate[pos=0.5] (-left)
(0.6,0) .. controls (0.6,-0.3) and (0.08,-0.2) ..
(0.05,-1)
coordinate[pos=0.5] (-right);
}
}
\begin{document}
\begin{tikzpicture}
% draw the axis with labels
\draw [Stealth-Stealth] (0,4) node[left]{$y$} |- (6,0) node[below] {$x$};
% draw the straight line plot
\draw [orange] plot[domain=0:6,samples=2](\x,1+\x*0.5);
% a loop to add the small distribution graphs and ticklabels on x-axis at three x-values
\foreach [count=\i] \x in {1.5,3,4.5} {
\draw (\x,1+\x*0.5) pic (p\i) {distribution}; % pic{distribution} draws the small picture defined above
\draw (\x,3pt) -- (\x,0) node[below] {$x_\i$}; % draw the tick and ticklabel
}
% draw arrows with annotations
\draw [-Stealth] (p3-right) to[out=-45,in=180] ++(0.8,-0.5) node[right] {Distribution of $\epsilon_3$};
\draw [Stealth-,shorten <=3pt] (p1-origin) -- ++(-1.7,0.2) node[left] {$E(Y_1) = \beta_0 + \beta_1X$};
\end{tikzpicture}
\end{document}
答案2
我会使用 PSTricks。以下是示例代码:
\documentclass[12pt]{article}
\usepackage{pst-all,pst-infixplot,pst-blur}
\begin{document}
\begin{center}
\begin{pspicture}(-1,-1.5)(3.5,1.5)
\psline{->}(0,0)(0,4)\rput[r](-0.1,3.8){$y$}
\psline{->}(0,0)(4,0)\rput[t](3.8,-0.1){$x$}
\psline[linecolor=orange]{-}(0,1)(4,3)
\psset{plotpoints=1000}
\infixtoRPN{0.4*2.7^(-10*x*x)}
\psline(1,0.5)(1,2.5)
\rput[l]{-90}(1,1.5){\psplot[linecolor=blue]{-0.8}{0.8}{\RPN}}
\pnode(1,1.5){k1}
\psline(2,1)(2,3)
\rput[l]{-90}(2,2){\psplot[linecolor=blue]{-0.8}{0.8}{\RPN}}
\pnode(2,2){k2}
\psline(3,1.5)(3,3.5)
\rput[l]{-90}(3,2.5){\psplot[linecolor=blue]{-0.8}{0.8}{\RPN}}
\pnode(3,2.5){k3}
\pnode(3.2,2.8){p1}
\rput[r]{0}(-0.2,2){\ovalnode[shadow=true,blur=true]{E}{$E[Y_1]=\beta_0+\beta_1\,X_1$}}
\ncarc{->}{E}{k1}
\rput[l]{0}(4,3){\ovalnode[shadow=true,blur=true]{dis}{Distribution of $\epsilon_3$}}
\rput(1,0){$|$}\rput[t](1,-0.1){$x_1$}
\rput(2,0){$|$}\rput[t](2,-0.1){$x_2$}
\rput(3,0){$|$}\rput[t](3,-0.1){$x_3$}
\ncarc{->}{dis}{p1}
\end{pspicture}
\end{center}
\end{document}
\endinput