11

I have the following code:

\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}

I need to place the formula $\exp(\frac{z_i-z_j}{2\sigma})$ pointing towards the middle of the exponential curve. Thx!

  • 1
    N.B. You should not use math-mode for italics. I've revised your code for this, and also to remove the scroll-bars, but without changing much else. – Niel de Beaudrap Apr 16 '15 at 23:34
5

This is just one possibility.

\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}

enter image description here

  • 2
    $potential$ --> potential and $cost$ --> cost. – Svend Tveskæg Apr 17 '15 at 0:30
  • 1
    The original had the dollar signs in it before my edit, this is not really Ignasi's fault. – Niel de Beaudrap Apr 17 '15 at 6:09
5

You can control the position of nodes most easily, in this case, by using the co-ordinate system of the plot itself to define co-ordinates/nodes, and drawing them in the normal TiKZ style.

Source

\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}

Result

Plot with a label and an arrow

4

A PSTricks solution:

\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}

output

2

A MetaPost solution, for whom it interests. The middle of a path is easily found with the Metafun format of MetaPost: it is point .5 along curve if curve is the name of the path. For MetaPost experts, here is the definition of the along binary operator in the Metafun format:

primarydef pct along pat = % also negative
    (arctime (pct * (arclength pat)) of pat) of pat
enddef ; 

Metafun also defines a handy function macro which produces the curve associated to a function (or a parametric curve), which I used here. It also defines some macros to produce grids, but I find them awkward to handle, so I devised my own macro here, gridlines, inspired by the mfpic package.

Since I do not like labels crisscrossed by grids or curves, I also defined a macro clearing the bounding box of each label, clearlabel.

To be processed with LuaLaTeX. (Needs version 2.10.1 of the luamplib package or higher.)

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.