# Displaying a pgfplots graph above another pgfplots graph in TikZ

I have two graphical solutions to an inequality (and an algebraic solution to it). One solution is to be displayed on the number line, and the other solution is to be displayed on the Cartesian plane. I want the number line and the x-axis in the Cartesian plane to be aligned vertically.

The inequality is

\begin{equation*} \frac{6}{x - 5} \geq x \end{equation*}

and the solution set is $(-\infty,-1) \cup (5,6]$. So, on the first graph, I want tick marks and labels for -1, 5, and 6 and a thick line drawn to the left of -1 and between 5 and 6. With TikZ, I used the following commands.

\draw[line width=2pt, -latex] ($(-1,0) +(-3pt,0pt)$) -- (-2.9,0);
\draw[fill] (-1,0) circle (3pt);

\draw (5,0) circle (3pt);
\draw[fill] (6,0) circle (3pt);
\draw[line width=2pt] ($(5,0) +(3pt,0pt)$) -- ($(6,0) +(-3pt,0pt)$);


I am not sure what the appropriate commands in pgfplots would be.

The code for the number line would not compile. I put comment lines in front of most of the commands for the number line.

By the way, I would like the points (-1,-1) and (6,6) marked with a black circle or a blue circle. Why is the point (-1,-1) marked with a blue diamond and the point (6,6) marked with a red circle?

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,positioning,intersections}

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

\begin{document}

\noindent {\textbf{1.) }}Determine the solution set to
\begin{equation*}
\frac{6}{x - 5} \geq x .
\end{equation*}
Graph the solution set on the real number line.
\vskip0.2in

\noindent {\bf Solution} \vskip1.25mm
\noindent $q(x) = 6/(x - 5)$ is a rational function defined on ${\mathbb{R}}\setminus\{5\}$. On the interval $(5, \, \infty)$, $q(x) \geq x$ if, and only if,
\begin{equation*}
(x - 6)(x + 1) = x^{2} - 5x - 6 \leq 0 ,
\end{equation*}
or equivalently, $5 < x \leq 6$. Likewise, on the interval $(-\infty, \, 5)$, $q(x) \geq x$ if, and only if, $x \leq -1$. \rule{1.5ex}{1.5ex}
\vskip0.2in

\noindent \hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[name=plot1, %height=0.5in, width=5in, clip=false,
%    xmin=-10,xmax=16,
%    xlabel=$x$,
%    axis line style={latex-latex},
%    xtick={},
%    extra x ticks={-1,5,6},
%    extra x tick labels={$-1$,5,6},
%    extra x tick style={xticklabel style={below}},
%    xlabel style={at={(ticklabel* cs:1)},anchor=north west}
]

\end{axis}
\begin{axis}[name=plot2, at={($(plot1.south)+(0,-0.75cm)$)}, anchor=north, height=5in, width=5in,
axis lines=middle, clip=false,
xmin=-10,xmax=16,
ymin=-10,ymax=16,
restrict y to domain=-10:16,
xtick={\empty},ytick={\empty},
ticklabel style={font=\tiny,fill=white},
extra x ticks={-1,6},
extra x tick labels={$-1$,6},
axis line style={latex-latex},
xlabel=$x$,ylabel=$y$,
enlargelimits={abs=0.25cm},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=501,domain=5.4:16, blue] {6/(x - 5)} node[above,pos=0.9,font=\footnotesize]{$y=\dfrac{6}{x-5}$};

\addplot[latex-latex, samples=2, domain=-10:15] {x} node[anchor=west, pos=1, font=\footnotesize]{$y=x$};
\addplot[dashed, latex-latex, samples=2, domain=-10:16] (5,x) node [pos=0.05, anchor=north, font=\footnotesize, sloped] {$x=5$};

\addplot coordinates{(6,6)} node[anchor=west,font=\footnotesize]{$(6,6)$};
\end{axis}

\end{tikzpicture}

\end{document}


PGFplots allows you to use all the usual functions from TikZ within its {axis} environment. You have access to the coordinate system through axis cs so that \node at (axis cs: 3, 4) {}; places a node at the x-y coordinate (3, 4). In version 1.11, axis cs became the default coordinate system used by TikZ within the {axis} environments so you need not specify axis cs every time and instead can just type \node at (3, 4) {};.

I provide below two very similar ways of drawing (what I think) you want. Both of them plot the two relevant curves (x and 6 / (5 - x)), but the first one also uses the x-axis as the number line whilst the second one places the number line above the the plot.

## Version 1: All in One

This solution uses the one set of axes to both display the appropriate equations for the inequality and label the part of the number line for which the inequality holds true:

\documentclass{amsart}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{thmtools}

\declaretheoremstyle[
numbered=unless unique,
bodyfont=\normalfont,
spaceabove=1em plus 0.75em minus 0.25em,
spacebelow=1em plus 0.75em minus 0.25em,
qed={\rule{1.5ex}{1.5ex}},
]{solstyle}

\declaretheorem[
style=solstyle,
title=Solution,
refname={solution,solutions},
Refname={Solution,Solutions}
]{solution}

\usepackage{enumitem}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{enumerate}[label=\bfseries\arabic*)]
\item Determine the solution set to
\begin{equation*}
\frac{6}{x - 5} \geq x .
\end{equation*}
Graph the solution set on the real number line.

\begin{solution}
We first observe that there is a singularity at $$x = 5$$ as we consider the
region above and below $$5$$ separately:
\begin{description}
\item[$$\boldsymbol{x > 5}$$] Over this interval, the denominator is always
greater than zero.  As a result, multiplying both sides by $$x-5$$ we
obtain:
\begin{align*}
& 6 \geq x^{2} - 5x \\
\Leftrightarrow & 0 \geq x^{2} - 5x - 6 = (x-6)(x+1)
\end{align*}
Over the given domain, $$x+1$$ is always positive; therefore, we must have
that $$x-6 \leq 0$$ and conclude that the inequality is satisfied only for
$$5 < x \leq 6$$.
\item[$$\boldsymbol{x < 5}$$] Over this internal, the denominator is always
less than zero.  As a result, multiplying both sides by $$x-5$$ flips the
inequality and we obtain:
\begin{align*}
& 6 \leq x^{2} - 5x \\
\Leftrightarrow & 0 \leq x^{2} - 5x - 6 = (x-6)(x+1)
\end{align*}
Over the given domain, $$x-6$$ is always negative; therefore, we must have
that $$x+1 \leq 0$$ and conclude that the inequality is satisfied only for
$$x \leq -1$$.
\end{description}
The two relevant curves for this inequality are plotted below with the
appropriate domain marked in red along the $$x$$-axis:
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
height=0.7\linewidth,
axis lines=middle,
xlabel=$$x$$,
ylabel=$$y$$,
xlabel style={at={(ticklabel* cs:1)},anchor=west},
ylabel style={at={(ticklabel* cs:1)},anchor=south},
clip=false,
domain=-5:10,
samples=501,
restrict y to domain=-10:16,
clip=false,
]

node [above, pos=0.95, font=\footnotesize] {$$y=\dfrac{6}{x-5}$$};
node[anchor=west, pos=1, font=\footnotesize]{$$y=x$$};
\draw [dashed, latex-latex]
(5,\pgfkeysvalueof{/pgfplots/ymin}) -- (5, \pgfkeysvalueof{/pgfplots/ymax})
node [pos=0.05, below, sloped, font=\footnotesize] {$$x=5$$};

\fill [blue] (-1, -1) circle [radius=2pt]
node [anchor=north, font=\footnotesize] {$$(-1, -1)$$};
\fill [blue] (6, 6) circle [radius=2pt]
node [anchor=west, font=\footnotesize] {$$(6, 6)$$};

\draw [-latex, red, very thick] (-1, 0) -- (\pgfkeysvalueof{/pgfplots/xmin}, 0);
\draw [red, very thick] (5, 0) -- (6, 0);
\fill [black] (-1, 0) circle [radius=2pt];
\draw [draw=black, fill=white] (5, 0) circle [radius=2pt];
\fill [black] (6, 0) circle [radius=2pt];
\end{axis}
\end{tikzpicture}
\end{center}
\end{solution}

\end{enumerate}
\end{document}


## Version 2: Number line on top

If you want to have the number line separate from the axis (as you intend to in the original question), you basically had it all right:

\begin{center}
\begin{tikzpicture}
\begin{axis}[
name=plot1,
width=\linewidth,
height=11em,
axis x line=middle,
axis y line=none,
clip=false,
domain=-5:10,
axis line style={latex-latex},
]

\draw [-latex, red, very thick] (-1, 0) -- (\pgfkeysvalueof{/pgfplots/xmin}, 0);
\draw [red, very thick] (5, 0) -- (6, 0)
node [above, pos=0] {$$5$$}
node [above, pos=1] {$$6$$};
\fill [black] (-1, 0) circle [radius=2pt]
node [red, above] {$$-1$$};
\draw [draw=black, fill=white] (5, 0) circle [radius=2pt];
\fill [black] (6, 0) circle [radius=2pt];
\end{axis}
\begin{axis}[
at=(plot1.south),
anchor=north,
width=\linewidth,
height=0.7\linewidth,
axis lines=middle,
xlabel=$$x$$,
ylabel=$$y$$,
xlabel style={at={(ticklabel* cs:1)},anchor=west},
ylabel style={at={(ticklabel* cs:1)},anchor=south},
clip=false,
domain=-5:10,
samples=501,
restrict y to domain=-10:16,
clip=false,
]

node [above, pos=0.95, font=\footnotesize] {$$y=\dfrac{6}{x-5}$$};
node[anchor=west, pos=1, font=\footnotesize]{$$y=x$$};
\draw [dashed, latex-latex]
(5,\pgfkeysvalueof{/pgfplots/ymin}) -- (5, \pgfkeysvalueof{/pgfplots/ymax})
node [pos=0.05, below, sloped, font=\footnotesize] {$$x=5$$};

\fill [blue] (-1, -1) circle [radius=2pt]
node [anchor=north, font=\footnotesize] {$$(-1, -1)$$};
\fill [blue] (6, 6) circle [radius=2pt]
node [anchor=west, font=\footnotesize] {$$(6, 6)$$};
\end{axis}
\end{tikzpicture}
\end{center}


### Extra Notes

Firstly, I took the liberty of cleaning up your example and make use of environments such as enumerate, description and created a solution environment to take care of the formatting for you automatically. Although having \texbf{1) } and \vskip1em do work, it isn't really the best way to use LaTeX. You should write what you mean instead of writing what you want to see. That is, instead of \textbf{1) }, \textbf{2) }, have an enumerated list; and instead of \textbf{Solution: } ... \rule{1.5ex}{1.5ex}, have a {solution} environment.

The advantage of writing what you mean is that if you want to change the way solutions look, you can do it in one place instead of having to go through your whole document and changing every instance.

A few other small things:

• For some reason, the {axis} environment seems to require having at least one \addplot command. I suspect that it is because it needs that to calculate the range of both axes even if xmin, xmax, ymin and ymax are all specified. Since I don't want to actually plot anything for the number line, I used \addplot [draw=none] {0};. I can't seem to find any mention of this requirement within the PGFplots documentation.
• When PGFplots calculates the positioning of all the labels, it seems to require a minimum height. When drawing the number line, I initially used a height=0pt, but this resulted in errors so instead I used height=11em. This has the additional benefit that I no longer need adjust the plot1.south coordinate as the vertical height of the baseline is enough.
• Instead of declaring samples and domain with every \addplot call, I declare these properties for the whole axis. This makes the code a little cleaner and also ensures that all the plots are drawn over the whole domain (for example, I'd rather not have the line y=x stop half-way). If that is intended behaviour though, having \addplot [domain=-5:0] {x}; will override the axis-wide domain.
• Similar to the previous note, having restrict y to domain in the {axis} options make that change work for every \addplot command in that environment. In addition, restrict y to domain discards points which are outside of the specified domain. You don't need to plot 6 / (5-x) in two separate \addplot calls because any value which end up outside of the specified y domain are automatically discarded.
• With regards to the two previous points, think of domain and restrict y to domain as settings the overall view port for the whole graph, and PGFplots will then figure out what to draw.
• I use \pgfkeysvalueof{/pgfplots/xmin} (and analogous) in order to obtain the value of xmin, ymin and ymax instead of hard-coding them. This means that if I want to change where the y-axis starts and stops, the asymptote line will automatically adjust.
• Instead of using \addplot to draw the line x=5, I use explicit coordinates. This is mostly because I found the PGFplots behaviour to be slightly inconsistent sometimes.
• Instead of using \addplot coordinates{-1,-1}; to draw a single point, I used one of the basic TikZ commands. Firstly, we aren't really plotting another curve, but instead annotating it, so \addplot already doesn't feel like what we need. Additionally, having the extra \addplot command will mess around with legend entries and the plot style cycle, hence why you initial plot had various shapes and colours despite you not specifying them.
• I chose width=\linewidth so that the plot fills the width of the current line. As for height=0.7\linewidth, it is arbitrary (I could have used height=5cm) but the rationale for using \linewidth is that if I change the formatting of the document, the aspect ratio of the plot width and height remains the same and it is always guaranteed to take up the width of the line. As for the 0.7 in particular, I typically use 0.62 because that ensure the plot follows the golden ratio, but in the particular case of this graph I thought it looked a little too squashed so instead I used 0.7.
• @J.P. Ellis I appreciate your detailed response. Are you saying that my code didn't compile because I was missing something like \addplot[draw=none] {0};'? Where is that in the manual?! Feb 21, 2016 at 18:00
• @J.P. Ellis I see that you used \fill[blue] (-1,-1) circle[radius=2pt] node[anchor=north] {$(-1,-1)$}; to draw the point of intersection of the graphs at (-1, -1). Why didn't \addplot coordinates{-1,-1}; draw a point at (-1,-1)? Feb 21, 2016 at 18:00
• @J.P. Ellis I like to manually encode the domain of the functions that I am plotting because I want them to be consistent with each other - same distance above or below x-axis or same distance to the left or to the right of y-axis. In case the graphs have asymptotes, I make a decision based on aesthetics the domain of the function. Feb 21, 2016 at 18:00
• I have some questions on your code. Why did you chooseheight=0.7\linewidth for the first graph? Why bother specifying a height, and if you do bother, why did you choose 0.7\linewidth? Does at={($(plot1.south) +(0,-0.75cm)$)} put the top of the second graph 0.75cm below the bottom of the first graph? Since I want to keep domain=-10:4.4 and domain=5.4:16 for the two parts of the graph of y=6/(x-5), how do I encode the domain for the number line in the first graph? Feb 21, 2016 at 18:01
• @Adelyn I have updated the answer to address the questions you have. Regarding the 3rd comment, you should think of domain and restrict y to domain as setting the overall view port and you let PGFplots figure things out. Also, I don't understand "Since I want to keep domain=-10:4.4 and domain=5.4:16 for the two parts of the graph of y=6/(x-5), how do I encode the domain for the number line in the first graph?". Your domain really is -10:16; however, you just don't want to clip points which are near the asymptote because the diverge and that's what restrict y to domain` is for. Feb 22, 2016 at 1:50