1

I have two graphs drawn using pgfplots. The graphs are drawn as I would like them to be drawn, and the x-axes are aligned, as I would like them to be aligned. There are three modifications that I need. First, the two graphs should be horizontally centered. Second, more (horizontal) space is needed between the two graphs. Third, a title needs to be centered over the y-axis of each graph. Something like the following code would be suitable.

\node[align=center, font=\bfseries, inner sep=0pt, above=3mm] at (current bounding box.north) {The graph of an \\ increasing function};

\node[align=center, font=\bfseries, inner sep=0pt, above=3mm] at (current bounding box.north) {The graph of a strictly \\ increasing function};

By the way, in the first graph, I have a horizontal line drawn along the x-axis between the origin and (1,0). Are the axes drawn with a width of 0.8pt? I used the option line width=1.2pt to draw this horizontal line. Is that typical when "drawing over" the axes?

\documentclass{amsart}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot [domain=-3:-2] {-3};
\addplot [domain=-2:-1] {-2};
\addplot [domain=-1:0] {-1};
\addplot [line width=1.2pt, domain=0:1] {0};
\addplot [domain=1:2] {1};
\addplot [domain=2:3] {2} node [pos=1, anchor=south west, font=\footnotesize] {$y=f(x)$};

\draw[fill] (-3,-3) circle [radius=1.5pt];
\draw[fill] (-2,-2) circle [radius=1.5pt];
\draw[fill] (-1,-1) circle [radius=1.5pt];
\draw[fill] (0,0) circle [radius=1.5pt];
\draw[fill] (1,1) circle [radius=1.5pt];
\draw[fill] (2,2) circle [radius=1.5pt];

\draw [fill=white] (-2,-3) circle [radius=1.5pt];
\draw [fill=white] (-1,-2) circle [radius=1.5pt];
\draw [fill=white] (0,-1) circle [radius=1.5pt];
\draw [fill=white] (1,0) circle [radius=1.5pt];
\draw [fill=white] (2,1) circle [radius=1.5pt];
\draw [fill=white] (3,2) circle [radius=1.5pt];

\end{axis}
\end{tikzpicture}
%
%
\begin{tikzpicture}
\begin{axis}[width=3in, clip=false,
    xmin=-3,xmax=3,
    ymin=-10,ymax=10,
    restrict y to domain=-8:8,
   % enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot [domain=-2:2] {x^3} node [pos=1, anchor=west, font=\footnotesize] {$y=g(x)$};

\end{axis}
\end{tikzpicture}

\end{document}

2 Answers 2

1

All of these things are easily achievable by combining the plots into a single tikzpicture.

"First, the two graphs should be horizontally centered."

This is done by surrounding the tikzpicture environment with the center environment.

"Second, more (horizontal) space is needed between the two graphs."

By placing the two plots in the same tikzpicture, we can control their relative positioning by naming the first plot via the name = <name> option, and then setting the location of the second plot with at = (<name>.east). Adding anchor = west keeps the x-axis alignment. After doing this, the separation distance between the plots can be adjusted via xshift = <value>.

"Third, a title needs to be centered over the y-axis of each graph. "

We can just use the title = {<title>} option. Note that I added align = center in order to achieve the manual line break you wanted.

Update: Title positioning adjustments

We can add some options in the title style to place the title below the axis.

Code:

\documentclass{amsart}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west},
    title style = {align = center, at = (current axis.south), anchor = north, yshift = -2em},
    title = {The graph of an \\ increasing function}, name = left
]

\addplot [domain=-3:-2] {-3};
\addplot [domain=-2:-1] {-2};
\addplot [domain=-1:0] {-1};
\addplot [line width=1.2pt, domain=0:1] {0};
\addplot [domain=1:2] {1};
\addplot [domain=2:3] {2} node [pos=1, anchor=south west, font=\footnotesize] {$y=f(x)$};

\draw[fill] (-3,-3) circle [radius=1.5pt];
\draw[fill] (-2,-2) circle [radius=1.5pt];
\draw[fill] (-1,-1) circle [radius=1.5pt];
\draw[fill] (0,0) circle [radius=1.5pt];
\draw[fill] (1,1) circle [radius=1.5pt];
\draw[fill] (2,2) circle [radius=1.5pt];

\draw [fill=white] (-2,-3) circle [radius=1.5pt];
\draw [fill=white] (-1,-2) circle [radius=1.5pt];
\draw [fill=white] (0,-1) circle [radius=1.5pt];
\draw [fill=white] (1,0) circle [radius=1.5pt];
\draw [fill=white] (2,1) circle [radius=1.5pt];
\draw [fill=white] (3,2) circle [radius=1.5pt];

\end{axis}
\begin{axis}[width=3in, clip=false,
    xmin=-3,xmax=3,
    ymin=-10,ymax=10,
    restrict y to domain=-8:8,
   % enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west},
    title style = {align = center, inner sep = 0pt, above = 3mm},
    title = {The graph of a strictly \\ increasing function},
    at = (left.east), anchor = west, xshift = 2cm
]
\addplot [domain=-2:2] {x^3} node [pos=1, anchor=west, font=\footnotesize] {$y=g(x)$};

\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

Output:

output

9
  • I need more vertical space between the titles and the labels for the y-axes. I had suggested options ` inner sep=0pt` and above=3mm. Are the axes drawn with a width of 0.8pt?
    – Adelyn
    Mar 12, 2016 at 17:29
  • @Adelyn Check out this answer. Is that what you want? Mar 12, 2016 at 18:13
  • Based on the post that you suggested and a comment from Zarko, I guess that title style={align=center, font=\bfseries,above=3mm} or title style={align=center, font=\bfseries, yshift=3mm} is what I need. Is the anchor for the title node at the south by default? I added anchor=north, inner sep=0pt to the title style= and got the same display.
    – Adelyn
    Mar 12, 2016 at 22:07
  • Yes, the anchor is south by default, but this is relative to the coordinate (current axis.north). The anchor is not relative to the entire axis, so setting anchor = north probably resulted in the title just looking like it was pushed lower. Check out page 342 of the pgfplots manual. In your title style, add at = (current axis.south) to place the title below the axis, and then you will want to change the anchor to anchor = north, and fine tune with a yshift. I'll update my answer above with this change. Mar 12, 2016 at 22:23
  • 1
    @Adelyn What I meant is that the anchor location is at the top of the y-axis, so that if you only specified anchor = north, this will place the title text directly below the top of the y-axis, not below the entire axis. This is why we needed to use at = (current axis.south). Mar 13, 2016 at 0:18
0
  1. For bigger distance between graph just ad some space between the, for example \qquad or as it is in code below \hspace{4em} (or other distance according to your wish). Note: your graphs are not centered in text width (if this matter).

  2. For title above graphs you need only to add to axis option title style={align=center, font=\bfseries} and title={... \\ ....}

  3. Third wish is already achieved with measure in second item :-)

All this gives:

enter image description here

The complete code is:

\documentclass{amsart}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.11}


    \begin{document}
\begin{tikzpicture}
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north east},
    ylabel style={at={(ticklabel* cs:1)},anchor=north east},
    title style={align=center, font=\bfseries},
    title={The graph of an \\ increasing function}
]

\addplot [domain=-3:-2] {-3};
\addplot [domain=-2:-1] {-2};
\addplot [domain=-1:0] {-1};
\addplot [line width=1.2pt, domain=0:1] {0};
\addplot [domain=1:2] {1};
\addplot [domain=2:3] {2} node [pos=1, anchor=south west, font=\footnotesize] {$y=f(x)$};

\draw[fill] (-3,-3) circle [radius=1.5pt];
\draw[fill] (-2,-2) circle [radius=1.5pt];
\draw[fill] (-1,-1) circle [radius=1.5pt];
\draw[fill] (0,0) circle [radius=1.5pt];
\draw[fill] (1,1) circle [radius=1.5pt];
\draw[fill] (2,2) circle [radius=1.5pt];

\draw [fill=white] (-2,-3) circle [radius=1.5pt];
\draw [fill=white] (-1,-2) circle [radius=1.5pt];
\draw [fill=white] (0,-1) circle [radius=1.5pt];
\draw [fill=white] (1,0) circle [radius=1.5pt];
\draw [fill=white] (2,1) circle [radius=1.5pt];
\draw [fill=white] (3,2) circle [radius=1.5pt];

\end{axis}
\end{tikzpicture}
%
\hspace{4em}
%
\begin{tikzpicture}
\begin{axis}[width=3in, clip=false,
    xmin=-3,xmax=3,
    ymin=-10,ymax=10,
    restrict y to domain=-8:8,
   % enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north east},
    ylabel style={at={(ticklabel* cs:1)},anchor=north east},
    title style={align=center, font=\bfseries},
    title={The graph of a strictly \\ increasing function}
]
\addplot [domain=-2:2] {x^3} node [pos=1, anchor=west, font=\footnotesize] {$y=g(x)$};

\end{axis}
\end{tikzpicture}
    \end{document}

About line widths: this depend on personal test. Different people, different taste ... If you liked more "bold" just increase their thickness. As I change position of axis labels according to my taste :-)

Edit: Little of topic: the first graph, as it is, can be drawn much simpler:

\documentclass{amsart}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.11}
    \usetikzlibrary{arrows.meta}

    \begin{document}
\begin{tikzpicture}[
    L/.style = {% as Line
                {Circle[width=1mm,length=1mm]}-%
                {Circle[width=1mm,length=1mm,open,line width=0.4pt]},
                shorten >=-0.6mm, shorten <=-0.6mm,
               }    ]
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north east},
    ylabel style={at={(ticklabel* cs:1)},anchor=north east},
    title style={draw,align=center, font=\bfseries},
    title={The graph of an \\ increasing function}
]

\draw[red,L] (-3,-3) edge (-2,-3)
             (-2,-2) edge (-1,-2)
             (-1,-1) edge (0,-1)
             (0,0) edge[thick] (1,0)
             (1,1) edge (2,1)
             (2,2)  --  (3,2) node[anchor=south west,font=\footnotesize] {$y=f(x)$};

\end{axis}
\end{tikzpicture}
     \end{document}

enter image description here

As you see, omitted as all \addplot ... and separated draw of circles are replaced with lines with filled and open circles on the ends.

And on-topic: On positioning of nodes you have more possibilities:

  • define anchor of node and say for example \node[anchor=south] at (<coordinate>) ... and node's anchor south will lie on <coordinate>

  • use option above, for example \node[above] at (<coordinate>) ... and node will lie on the same place as before

  • define anchor of node and after that use option above, for example \node[anchor=north,above] at (<coordinate>) ... and node will lie as before regardless that regarding to anchor one expect different (in this case above override anchor)

  • define anchor of node after use of option above, for example: \node[above,anchor=north] at (<coordinate>) .... Now the node position will differ from before cases. On <coordinate> will lie node's anchor north

With drawing nodes border (as shown on figure) can help to see, where is text baseline. Its position in node is depend on text depth, if it is not extra defined. For details is very recommencement to read TikZ manual "17.4.4 Text Parameters: Height and Depth of Text" on page 226. and than "17.5 Positioning Nodes" on page 227.

3
  • I copied your axis options for title style and title but I got the title typeset much closer to the y designating the yaxis. Did you get the display you posted just from your code?
    – Adelyn
    Mar 12, 2016 at 21:59
  • I got a display that is much closer to what you have when I added anchor=north, inner sep=0pt, above=3mm as options to title style=, and I got the same display when I replaced anchor=north with anchor=south. Why is the anchor value ignored?
    – Adelyn
    Mar 12, 2016 at 22:00
  • 1
    of course the image is generated by posted code. I tested with v. 1.11 as well with v 1.13. In both cases I got the same result. In graph I used default position for titles. If you like to change distance to graph, than the simple way is use \yshift= .... With above=... you always override anchors=.... In testing of different position try to ad option draw to tile options. With this you will better see, where the title is.
    – Zarko
    Mar 13, 2016 at 6:14

You must log in to answer this question.

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