我想绘制两个 beta 分布来比较先验分布和后验分布,但似乎系数对于 LaTeX 来说太大了?如何在不更改函数的情况下修复它?它有几个错误,包括Dimension too large
和Number too big
。
\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\definecolor{qqccqq}{rgb}{0,0.8,0}
\definecolor{qqttcc}{rgb}{0,0.2,0.8}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=8.333333333333332cm,y=1.0cm]
\draw[->,color=black] (-0.1,0) -- (1.1,0);
\foreach \x in {,0.2,0.4,0.6,0.8,1}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[color=black] (1.04,0.04) node [anchor=south west] { $\theta$};
\draw[->,color=black] (0,-0.5) -- (0,5.5);
\foreach \y in {,1,2,3,4,5}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-0.1,-0.5) rectangle (1.1,5.5);
\draw[dash pattern=on 1pt off 1pt,color=qqttcc, smooth,samples=100,domain=8.000000000003847E-7:0.9999990727280006] plot(\x,{3682570000*(1-(\x))^(10.67)*(\x)^(21.49)});
\draw[color=qqccqq, smooth,samples=100,domain=8.000000000003847E-7:0.9999990727280006] plot(\x,{2120.93*(1-(\x))^(2.67)*(\x)^(9.49)});
\draw (0.49,4.17) node[anchor=north west] {$g(\theta|D)$};
\draw (0.89,3.23) node[anchor=north west] {$g(\theta)$};
\end{tikzpicture}
\end{document}
答案1
还可以考虑使用 Asymptote 选项,因为它在图形绘制方面非常强大,尤其是在有曲线数组的情况下。下面的示例将函数组织在数组中;其他属性(如颜色、宽度、标签等)也可以组织在数组中,以便在一个循环中绘制。此外,y
标签的坐标计算为曲线上的一个点。
asy-plot.tex
\documentclass{standalone}
\usepackage{lmodern}
\usepackage[inline]{asymptote}
\begin{document}
\begin{asy}
import graph;
size(250,170,IgnoreAspect);
real xMin=8.000000000003847e-7;
real xMax=0.9999990727280006;
int n=100;
typedef real Func(real x);
Func[] f={
new real(real x){return 3682570000*(1-x)^(10.67)*x^(21.49);},
new real(real x){return 2120.93*(1-x)^2.67*x^9.49;},
};
pen dashed=linetype(new real[] {4,3}); // set up dashed pattern
pen[] curvePen={rgb(0,0.8,0)+dashed,rgb(0,0.2,0.8),};
real[] curveWidth={1.6pt,2pt};
Label[] lab={
Label("$g(\theta\,\vert D)$",(0.6,f[0](0.6))),
Label("$g(\theta)$",(0.9,f[1](0.9))),
};
for(int i=0;i<f.length;++i){
draw(graph(f[i], xMin ,xMax ,n),curvePen[i]+curveWidth[i]);
label(lab[i],UnFill(2pt));
}
xaxis("",0,1.049,RightTicks(Step=0.1,step=0.05),EndArrow);
yaxis("",0,5-0.15,LeftTicks(Step=1,step=0.5),EndArrow);
\end{asy}
\end{document}
为了处理它latexmk
,请创建文件latexmkrc
:
sub asy {return system("asy '$_[0]'");}
add_cus_dep("asy","eps",0,"asy");
add_cus_dep("asy","pdf",0,"asy");
add_cus_dep("asy","tex",0,"asy");
并运行latexmk -pdf asy-plot.tex
。结果如下
答案2
虽然不是直接的答案,但我会用pgfplots
它。也许在正确的时间点打开fpu
库可能会解决问题,但您不需要绘制任何轴等。所以我认为这更方便。
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\definecolor{qqccqq}{rgb}{0,0.8,0}
\definecolor{qqttcc}{rgb}{0,0.2,0.8}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
\begin{axis}[
axis lines=middle,
every inner x axis line/.append style={-triangle 45},
every inner y axis line/.append style={-triangle 45},
enlargelimits]
\addplot[dash pattern=on 1pt off 1pt,color=qqttcc, smooth,samples=100,domain=8.000000000003847E-7:0.9999990727280006] plot(\x,{3682570000*(1-(\x))^(10.67)*(\x)^(21.49)});
\addplot[color=qqccqq, smooth,samples=100,domain=8.000000000003847E-7:0.9999990727280006] plot(\x,{2120.93*(1-(\x))^(2.67)*(\x)^(9.49)});
\node at (axis cs:0.49,4.17) {$g(\theta|D)$};
\node at (axis cs:0.89,3.23) {$g(\theta)$};
\end{axis}
\end{tikzpicture}
\end{document}
与您的编辑相关的问题是,数字在域的末尾变得太小。为了显示,我已在 y 轴上切换到对数轴。TeX 无法处理该精度,但fpu
TikZ 库或pgfplots
默认情况下可以处理一定程度并以除以零的错误退出。否则pgfplots
允许gnuplot
在 TeX 之外执行指令。请查看手册,pgfplots
此外,您不需要定义使用的每种颜色。xcolor 是一个非常强大的包,因此您可以使用颜色混合功能。这是一个没有任何轴设置等的最小示例,以显示使用起来多么容易:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8} % The latest version on CTAN
\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}
\addplot[red,smooth,samples=100,domain=0.0:1.0] {(3.1415926535*sin(((x)*3.1415926535)*180/pi))/2};
\addplot[green!80!black, smooth,samples=100,domain=0.0:1.0] {(x)^1^2*(1-(x))^8*sin(((x)*3.1415926535)*180/pi)*1/(3.44*10^(-7))};
\addplot[blue!80!green, ultra thick,smooth,samples=100,domain=0.0:1.0] {2.718281828^((-((x)-0.58)^2)/(0.1^2*2))/(abs(0.1)*sqrt(3.1415926535*2))};
\end{semilogyaxis}
\end{tikzpicture}
\end{document}