0

I have drawn the following figure in Mathematica. It is plot of y <= 1/x

enter image description here

But when I inserted it in latex using \begin{figure}, it was inserted there but I could not control its position in my document. Sometimes it moves into the unwanted position.

So, I thought if I could draw the figure in Latex using tikz. Actually, I want to draw the following figure ...

enter image description here

Can I draw the same graph as above in Latex?

Please help me.

  • 1
    Well, yes, you can probably recreate that with e.g. pgfplots, but if the positioning is the only reason you have for doing that, why not ask how to control the position of a floating environments like figure? See https://tex.stackexchange.com/q/8625 https://tex.stackexchange.com/q/2275 https://tex.stackexchange.com/q/279 https://tex.stackexchange.com/q/39017 Note also that you do not need a figure environment to insert images. If you just want to insert a centered image at a specific point in the text, without a caption, use \begin{center} \includegraphics{filename} \end{center}. – Torbjørn T. Jul 25 '21 at 08:55

1 Answers1

1

If you want to draw it in TikZ only, without using the axis environment (which I don't use, generally), here's a way to do that. It's what I use for my students.

fill under the inverse function

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{arrows.meta}

\begin{document} \begin{tikzpicture} \def\xmin{-9} \def\xmax{8} \def\ymin{-4} \def\ymax{6} \def\Mgrid{1} \def\mgrid{0.2}

    \def\f{1/x}

    \draw[cyan!20,thin] (\xmin,\ymin) grid[step=\mgrid] (\xmax,\ymax);
    \draw[cyan] (\xmin,\ymin) grid[step=\Mgrid] (\xmax,\ymax);
    \draw[-Stealth] (\xmin,0) -- (\xmax,0);
    \draw[-Stealth] (0,\ymin) -- (0,\ymax);
    \foreach \x in {\xmin,...,-1,1,2,...,\xmax} \node[below] at (\x,0) {\footnotesize \strut \x};
    \foreach \y in {\ymin,...,-1,1,2,...,\ymax} \node[left] at (0,\y) {\footnotesize \strut \y};

    \clip (\xmin,\ymin) rectangle (\xmax,\ymax);

    \fill[red,opacity=0.3] (\xmin,\ymin) -- plot[domain=\xmin:-0.01,samples=100] ({\x}, {1/\x}) -- cycle;       
    \draw[thick,domain=\xmin:-0.01,red,samples=100] plot ({\x}, {1/\x});

    \fill[red,opacity=0.3] (0,\ymin) -- (0,\ymax) -- plot[domain=0.01:\xmax,samples=100] ({\x}, {1/\x}) -- (\xmax,\ymin) -- cycle;      
    \draw[thick,domain=0.01:\xmax,red,samples=100] plot ({\x}, {1/\x});

\end{tikzpicture}

\end{document}

SebGlav
  • 19,186