2

I am trying to reproduce the picture below on the left with the packages "tikz" and "pgfplots". But as you can see the best I got was the figure below on the right. In my attempt I modified the tex code from here and adapted it as follows below after the pictures.

My questions are as follows.

  1. How do I translate the "x" and "y" axes such that these axes intersect perpendicularly at the point (0,0)?

  2. How do I position (x,y) coordinates on the graph? How do I position the red circles on the graph?

  3. How do I control the size of the axis legends? And the size or width of the axes? How to change it?

enter image description here

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat = newest}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin = -6, xmax = +8,
            ymin = -2.0, ymax = +11.0,
            xtick distance = 1.0,
            ytick distance = 1.0,
            grid = both,
            minor tick num = 1,
            major grid style = {lightgray!25},
            minor grid style = {lightgray!25},
            width =  1.0\textwidth,
            height = 1.0\textwidth,
            xlabel = {$x$},
            ylabel = {$y$},
            legend cell align = {left},
        ]
            \addplot[
                domain = -4:3,
                samples = 200,
                smooth,
                blue,
                thick,
            ] %{exp(ln(x+1)/ln(2))};
            {2^(x)+1};
            
            \addplot[
                domain = -4:3,
                samples = 200,
                smooth,
                red,
                dashed
            ] {2^(x)};
            
            %\legend{Plot from expression, Plot from file}
        \end{axis}
    \end{tikzpicture}
\end{document}
3
  • You have already changed the height and width of the axis, as you can see from your code, just above the xlabel. Commented Jul 14, 2022 at 19:23
  • @TorbjørnT. Yes, I have tried. If I change width = 1 to width = 2 it only changes the scale. It does not translate the axes into the graph. Commented Jul 14, 2022 at 19:45
  • Sorry for being slow, I don't understand what you mean by that last sentence. You asked how to change the size of the axis, that is what width and height does. Did you mean to ask something else? Commented Jul 14, 2022 at 19:46

1 Answer 1

2
  1. axis lines=center

  2. There are multiple ways of doing that, you could for example use TikZ code such as \node [circle, fill=red, minimum size=5pt, inner sep=0pt, label={[red]below right:$(0,1)$}] at (0,1) {};. However, I think I would rather use the features of pgfplots, specifically nodes near coords. See the code below for an example, ask if anything is unclear.

  3. For example legend style={font=\small} to reduce the font size of the legend. The rest of this point you have already answered in your code (the width and height keys)

output of the code below

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat = 1.18} % using an explicit version is recommended by the package author

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            axis lines=center, % axis lines through origin instead of box
            xmin = -6, xmax = +8,
            ymin = -2.0, ymax = +11.0,
            xtick distance = 1.0,
            ytick distance = 1.0,
            grid = both,
            minor tick num = 1,
            major grid style = {lightgray!25},
            minor grid style = {lightgray!25},
            width =  1.0\textwidth,
            height = 1.0\textwidth,
            xlabel = {$x$},
            ylabel = {$y$},
            legend cell align = {left},
            visualization depends on=x\as\tmpX, % make the x coordinate available for use in nodes near coords below
            visualization depends on=y\as\tmpY, % same for y
            legend style={
               font=\small, % change font size of legend
               at={(1,1)},
               anchor=north east
               }
        ]
            \addplot[
                domain = -4:3,
                samples = 200,
                smooth,
                blue,
                thick,
            ] %{exp(ln(x+1)/ln(2))};
            {2^(x)+1};
            
            \addplot[
                domain = -4:3,
                samples = 200,
                smooth,
                red,
                dashed
            ] {2^(x)};
            
            \addplot [
                blue,
                mark=*,
                only marks,
                samples at={0,1,2,3}, % specify x values to plot function at
                % now add a node next to each plotted point
                % we use the macros defined with visualization depends on here
                % to make the coordinate pair
                nodes near coords={$(\pgfmathprintnumber{\tmpX}, \pgfmathprintnumber{\tmpY})$},
                % finally define were the nodes are placed relative to the plotted points
                nodes near coords align=above left
                ] {2^(x)+1};
            

            % a more manual way of adding dots with coordinate label
            %\node [circle, fill=red, minimum size=5pt, inner sep=0pt, label={[red]below right:$(0,1)$}] at (0,1) {};
            
            \legend{Plot from expression, Plot from file}
        \end{axis}
    \end{tikzpicture}
\end{document}
1
  • 1
    @MathOverview \node at (3,3) {$y=2^x+1$}; for example. That will use the axis coordinates (assuming your compat level is set to 1.11 or higher, which it is in this case), if you want relative coordinates, where (0,0) is the lower left and (1,1) is upper right, prefix the coordinate with rel axis cs, e.g. \node at (rel axis cs:0.5, 0.5) {$y=2^x+1$}; Commented Jul 14, 2022 at 19:57

You must log in to answer this question.

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