3

While experimenting with PGFPlots, I found out a little weird issue:

I want to plot the function given by f(x) = 2^x, so I type:

\begin{tikzpicture}
        \begin{axis}[   axis x line = center,
                        axis y line = center,
                        xmin = 0,
                        xmax = 4,
                        xlabel = {$x$},
                        ylabel = {$y$},
                        ticks = none,
                        width=6cm]

            \addplot[   smooth,
                        domain = 0:3]
                plot (\x,{2^(\x)});
        \end{axis}
    \end{tikzpicture}

Which results in: enter image description here

At first, I am happy the code works but upon a closer look, I realize that PGF says f(0)= 2^0 = 0, which is obviously wrong, as it should be 1. I tried to play with the parenthesis in the line:

    plot (\x,{2^(\x)});

But it didn't work. What could be causing this? I have suspected that it's some kind of scaling issue (as if the plot was zoomed out a lot), since I used the width=6cm option, but removing that doesn't seem to solve it. Any suggestions?

  • 1
    put the ticks back on and you'll see and then add ymin=0 – percusse Mar 19 '16 at 4:18
5

As percusse already mentioned in his comment this is because of the combination of options you have (not) used, i.e. providing ticks=none but not setting ymin. Turning back on the ticks and/or setting ymin will show you, that PGFPlots calculates the right values.

Also I want to note that you can simplify the equation you give to the \addplot handler. "Your version" is mainly used for parametric plots.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            axis x line=center,
            axis y line=center,
            xmin=0,
            xmax=4,
            ymin=0,         % <-- added
            xlabel={$x$},
            ylabel={$y$},
%           ticks = none,
            smooth,
            domain=0:3,
        ]
        % original version
            \addplot []                          (\x,{2^(\x)});
        % simplified version
            \addplot+ [only marks,mark size=1pt] {2^x};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • 1
    plot is also not needed – percusse Mar 19 '16 at 6:49
  • 1
    @percusse, of course. I think I never use the "normal plot" handler, so I just forgot ;). Many thanks for the hint. I have updated my answer. – Stefan Pinnow Mar 19 '16 at 6:58
  • I'm really new to PGFPlots, so I wasn't aware these options were necessary to better build the plot. One question about your simplified version: What does the + after \addplot do? Also, why are you using [only marks, mark size = 1pt]? – Guilherme Vargas Mar 19 '16 at 13:23
  • @GuilhermeVargas, the + says that the following options should be appended to the default style of this plot; that is why for example the color is still red. Otherwise only the applied options are used for the plot. I added the options so that for that plot no line is drawn between the points (only marks) and I set the size of the markers, as the name suggests. – Stefan Pinnow Mar 19 '16 at 13:28
  • @GuilhermeVargas, if you are new to PGFPlots, I suggest reading/doing the tutorials (see chapter 3 in the manual). There you will learn a lot of the basics. Have fun !! – Stefan Pinnow Mar 19 '16 at 13:31

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.