1

I am new to Latex, and am drawing an exponential function like this: enter image description here

However, when my preview shows me this instead:

enter image description here

Here's my code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}
    \begin{axis}[axis x line=middle, axis y line=middle,
    ymax=18,
    ylabel=$y$, 
    xmin=0,xmax=5,xlabel=$x$,
    samples=201,
    legend pos=south east]
    \addplot [blue,thick,domain=0:4] {16*(0.5)^x};
    \end{axis}
    \end{tikzpicture} 
\end{figure}

How do I label correctly for the dotted line and their corresponding values on a and y axes?

1 Answer 1

2

You can use path picture for the clipped grid.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{figure}[!h]
    \centering
    \begin{tikzpicture}
    \begin{axis}[width=5cm,height=7cm,
    axis x line=middle, axis y line=middle,
    ymax=18,ymin=0,ylabel=$y$,ytick={1,2,4,8,16},
    xmin=0,xmax=5,xlabel=$x$,
    samples=201,
    legend pos=south east]
    \path [
     path picture={\foreach \X in {1,...,4}
      {\draw[dashed,gray,thin] (\X,\pgfkeysvalueof{/pgfplots/ymin})
       -- (\X,\pgfkeysvalueof{/pgfplots/ymax});}
      \foreach \Y in {1,2,4,8,16}      
      {\draw[dashed,gray,thin] (\pgfkeysvalueof{/pgfplots/xmin},\Y)
       -- (\pgfkeysvalueof{/pgfplots/xmax},\Y);}}]
     plot[smooth,domain=0:4] (\x,{16*(0.5)^\x}) -- (4.1,0)-| cycle ;
    \addplot [blue,thick,smooth,domain=0:4] {16*(0.5)^x} ;
    \end{axis}
    \end{tikzpicture} 
\end{figure}
\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .