0

I use this code for drawing plots.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{fancybox}

\begin{document}

    \begin{tikzpicture}
    \begin{axis}[axis lines=middle,samples=200]
    \addplot[blue,domain=-3:1.85] {1/(x-2) +3 };
    \addplot[blue,domain=2.15:6] {1/(x-2) + 3};
    \draw[red!20,dashed] (axis cs:2,-4) -- (axis cs:2,10);
    \end{axis}
    \node at (4.4,-0.6) {\ovalbox{one function}};
    \end{tikzpicture}

\end{document}

I want the node (e.g. \ovalbox{one function}) at the center of the image. I don't know how to do this automatically, so I do it as I assume it is right only by watching this. Is it possible to put it at the center automatically? enter image description here

  • 2
    \node at (current axis.center) {\ovalbox{one function}};. You can also use \node[draw,rounded corners] at (current axis.center) {one function};, that way you do not need the ovalbox. – user121799 Aug 1 at 17:46
  • @marmot , thanks for your answer! I meant "center at the bottom of the image". Your code puts the "one function" inside the image (ibb.co/k20ff43). If I replace center with north the "one function" is at the right place, but a part of the picture is missing (ibb.co/H7Y3zsC). – Kώστας Κούδας Aug 1 at 18:07
  • Then use \node at (current axis.south) {\ovalbox{one function}}; or \node[draw,rounded corners] at (current axis.south) {one function}; after \end{axis}, or with south replaced by north. You can also use clip=false and put the node within the axis. – user121799 Aug 1 at 18:17
3

The image defines a bounding box, just place the text to the south of this bounding box.

I anchor it to the north so that the text does not overlap the image.

\node[anchor=north] at (current bounding box.south) {\ovalbox{one function}};

screenshot

\documentclass{article}
\usepackage{pgfplots}
\usepackage{fancybox}

\begin{document}

    \begin{tikzpicture}
    \begin{axis}[axis lines=middle,samples=200]
    \addplot[blue,domain=-3:1.85] {1/(x-2) +3 };
    \addplot[blue,domain=2.15:6] {1/(x-2) + 3};
    \draw[red!20,dashed] (axis cs:2,-4) -- (axis cs:2,10);
    \end{axis}
    \node[anchor=north] at (current bounding box.south) {\ovalbox{one function}};
    \end{tikzpicture}

\end{document}
  • Voting down deserves an explanation. Without explanation this has no meaning other than gratuitous hostility. – AndréC Aug 2 at 17:04
6
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

    \begin{tikzpicture}
    \begin{axis}[axis lines=middle,samples=200]
    \addplot[blue,domain=-3:1.85] {1/(x-2) +3 };
    \addplot[blue,domain=2.15:6] {1/(x-2) + 3};
    \draw[red!20,dashed] (axis cs:2,-4) -- (axis cs:2,10);
    \end{axis}
    \node[draw,rounded corners,below=1pt] at (current axis.south) {one function};
    \end{tikzpicture}

\end{document}

enter image description here

0

I recommend using plain TikZ for plotting ordinary functions, to get the best picture as you like, in particular, adding some node at the center south of the current picture.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[yscale=.4,xscale=.7]
\draw[->] (-3,0)--(6,0) node[below]{$x$};
\draw[->] (0,-4)--(0,10) node[left]{$y$};
\draw[blue,smooth,samples=200] 
plot[domain=-3:1.85] (\x,{1/(\x-2)+3})
plot[domain=2.15:6] (\x,{1/(\x-2)+3});
\draw[red,dashed] (2,-4)--(2,10) (-3,3)--(6,3);
\path
(0,0) node[below left]{O}
(4.5,5) node[blue]{$y=3+\dfrac{1}{x-2}$}
(0,2.5) node[scale=.7,blue,below left]{$5/2$}
(5/3,0) node[scale=.7,blue,below left]{$5/3$}
(2,0) node[red,below right]{$2$}
(0,3) node[red,above left]{$3$};

\path (current bounding box.south) 
node[below=5mm,draw,rounded corners]
{a function with two asymptotes};
\end{tikzpicture}
\end{document}

PS: I know that pgfplots very useful (colormap, quiver, etc), but it make us loss of controls, or difficult to control. I will use pgfplots if plain TikZ is too long to draw.

For the answers using pgfplots here:

  • Should numbers -2,2,4,6,8 appear? Someone may say: "we can control appearance by adding some options inside axis environment", but it is not convernient. Moreover, numbers 5/2, 5/3 should be appear.
  • I put options [yscale=.4,xscale=.7] to make plain TikZ figure is almost same size with pgfplots figure. So pgfplots automatically scaled the figure. Sometimes this is good, sometimes it is not.
  • Removing option [axis lines=middle], appearance of the figure is totally changed. How would we want numbers 5.10.15 with big box around?

enter image description here

I put all code together for comparing.

\documentclass[border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=200]
\addplot[blue,domain=-3:1.85] {1/(x-2) +3 };
\addplot[blue,domain=2.15:6] {1/(x-2) + 3};
\draw[red] (axis cs:2,-4) -- (axis cs:2,10);
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,samples=200]
\addplot[blue,domain=-3:1.85] {1/(x-2) +3 };
\addplot[blue,domain=2.15:6] {1/(x-2) + 3};
\draw[red!20,dashed] (axis cs:2,-4) -- (axis cs:2,10);
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}[yscale=.4,xscale=.7]
\draw[->] (-3,0)--(6,0) node[below]{$x$};
\draw[->] (0,-4)--(0,10) node[left]{$y$};
\draw[blue,smooth,samples=200] 
plot[domain=-3:1.85] (\x,{1/(\x-2)+3})
plot[domain=2.15:6] (\x,{1/(\x-2)+3});
\draw[red,dashed] (2,-4)--(2,10) (-3,3)--(6,3);
\path
(0,0) node[below left]{O}
(4.5,5) node[blue]{$y=3+\dfrac{1}{x-2}$}
(0,2.5) node[scale=.7,blue,below left]{$5/2$}
(5/3,0) node[scale=.7,blue,below left]{$5/3$}
(2,0) node[red,below right]{$2$}
(0,3) node[red,above left]{$3$};
\end{tikzpicture}
\end{document}

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.