我有以下代码:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[grid=both,
axis x line=center,
axis y line=center,
black,
xlabel={\textit{potential}},
ylabel={\textit{cost}},
xlabel style={below right},
ylabel style={above center},
xmax=5,
ymax=2,
axis lines=middle,
enlargelimits
]
\addplot [mark=none, red, thick] {exp(-x)}
node [pos=1, pin=500:{%
\color{black}%
$\exp(\frac{z_i-z_j}{2\sigma})$%
}] {};
\end{axis}
\end{tikzpicture}
\end{document}
我需要将公式$\exp(\frac{z_i-z_j}{2\sigma})$
指向指数曲线的中间。谢谢!
答案1
这只是一种可能性。
\documentclass[tikz,border=3mm]{standalone}
%\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[grid=both, %grid=none
axis x line=center,
axis y line=center,
black,
xlabel={$potential$},
ylabel={$cost$},
xlabel style={below right},
ylabel style={above center},
xmax=5,
ymax=2,
axis lines=middle,
%restrict y to domain=-7:12,
enlargelimits,
domain=-.5:5
]
\addplot[mark=none, red, thick] {exp(-x)} coordinate[pos=0.3] (aux);
\node[anchor=south west, text=black] (a) at (axis cs:2,0.5) {$\exp(\frac{z_i-z_j}{2\sigma})$};
\draw (aux)--(a.west);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
在这种情况下,您可以最轻松地控制节点的位置,通过使用图本身的坐标系来定义坐标/节点,并以正常的 TiKZ 样式绘制它们。
来源
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[grid=both,
axis x line=center,
axis y line=center,
black,
xlabel={\textit{potential}},
ylabel={\textit{cost}},
xlabel style={below right},
ylabel style={above center},
xmax=5,
ymax=2,
axis lines=middle,
enlargelimits
]
\addplot [mark=none, red, thick] {exp(-x)};
\coordinate (P) at (axis cs:1,{exp(-1)});
\node (label) at (axis cs:3,{1+exp(-1)}) {$\exp(\frac{z_i-z_j}{2\sigma})$};
\draw [red!50!black, thick, dashed, ->, shorten >=2pt] (label) -- (P);
\end{axis}
\end{tikzpicture}
\end{document}
结果
答案3
PSTricks 解决方案:
\documentclass{article}
\usepackage{pst-plot}
% approximation to Euler's number
\def\PSEuler{2.71828182845904523536 }
% point on x-axis
\def\point{1.5 }
\begin{document}
\begin{figure}
\centering
\psset{yunit = 2}
\begin{pspicture}(-0.73,-0.27)(7.04,2.5)% found manually
\psaxes[Dy = 0.5]{->}(0,0)(-0.5,-0.25)(5.5,2.3)[Potential,0][Cost,90]
\psplot[linecolor = red]{-0.6}{5}{\PSEuler x neg exp}
\psline{->}%
(!\point 1 add \PSEuler \point neg 1 add exp)%
(!\point \PSEuler \point neg exp)
\uput[90](!\point 1 add \PSEuler \point neg 1 add exp)%
{$\displaystyle \exp{\mkern -8mu}\left(\frac{z_{i} - z_{j}}{2\sigma}
\right)$}
\end{pspicture}
\end{figure}
\end{document}
答案4
MetaPost 解决方案,它对感兴趣的人有用。路径的中间很容易找到元乐趣MetaPost 的格式:point .5 along curve
如果是路径的名称。对于 MetaPost 专家,以下是Metafun 格式中二元运算符curve
的定义:along
primarydef pct along pat = % also negative
(arctime (pct * (arclength pat)) of pat) of pat
enddef ;
Metafun 还定义了一个方便的function
宏,它生成与函数(或参数曲线)相关的曲线,我在这里使用了它。它还定义了一些宏来生成网格,但我发现它们很难处理,所以我在这里设计了自己的宏,gridlines
灵感来自mfpic
包裹。
由于我不喜欢标签被网格或曲线交叉,因此我还定义了一个宏来清除每个标签的边界框clearlabel
。
使用 LuaLaTeX 进行处理。(需要软件包 2.10.1luamplib
或更高版本。)
\documentclass[border=2mm]{standalone}
\usepackage{amsmath, luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\mplibnumbersystem{double}
\begin{document}
\begin{mplibcode}
u := 1.25cm; v := 2.5cm;
Xmin := -.75; Xmax := 5.4; Ymin := -.25; Ymax := 2.25;
xmin := Xmin; xmax := 5; xstep := .05; ystep := .5;
def gridlines (expr xmin, xmax, xstep)(expr ymin, ymax, ystep) =
for i = ceiling(xmin/xstep) upto xmax div xstep:
draw (i*xstep, ymin) -- (i*xstep, ymax) ;
endfor
for j = ceiling(ymin/ystep) upto ymax div ystep:
draw (xmin, j*ystep) -- (xmax, j*ystep);
endfor
enddef;
vardef clearlabel@#(expr str, pos) =
save pic; picture pic; pic = thelabel@#(str, pos);
unfill boundingbox pic enlarged bp; draw pic
enddef;
beginfig(1);
drawoptions(withcolor .8white);
gridlines(Xmin*u, Xmax*u, u)(Ymin*v, Ymax*v, .5v);
drawoptions();
drawarrow (Xmin*u, 0) -- (Xmax*u, 0); drawarrow (0, Ymin*v) -- (0, Ymax*v);
path curve;
curve = function(2, "x", "exp -x", xmin, xmax, xstep) xyscaled (u, v);
draw curve withcolor red;
picture mylabel;
mylabel = thelabel("$\exp\!\Big(\dfrac{z_i-z_j}{2\sigma} \Big)$",
.5(Xmax*u, Ymax*v));
drawarrow center bbox mylabel -- point .5 along curve;
unfill bbox mylabel; draw mylabel;
for i = 1 upto 5:
clearlabel.bot("$" & decimal i & "$", (i*u, 0));
endfor;
for j = .5 step .5 until 2:
clearlabel.lft("$" & decimal j & "$", (0, j*v));
endfor
label.top("Potential", (Xmax*u, 0));
label.lft("Cost", (0, Ymax*v));
endfig;
\end{mplibcode}
\end{document}