7

I am beginner (and it is my fourth article here) and I am struggling with plotting some basic graphs. I found here some similar articles, but it did not help me. :( I want to plot graph, where is: y>= (1/x) (blue) and x<=0 (red). I thank you in advance for any help.

\documentclass{article} 
\usepackage{pgfplots} 
\usetikzlibrary{patterns}
\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={ \pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style=opaque, grid=none}\pgfplotsdrawaxis}}}
\newcommand{\drawge}{-- (rel axis cs:5,0) -- (rel axis cs:5,5) -- (rel axis cs:0,5) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:5,5) -- (rel axis cs:5,0) -- (rel axis cs:0,0) \closedcycle}

\begin{document}

    \begin{figure}[htpb] 
        \centering 
        \begin{tikzpicture} 
            \begin{axis}[only axis on top,
            axis line style=very thick, 
            axis x line=middle, 
            axis y line=middle, 
            ymin=-3,ymax=5.99,xmin=-3,xmax=5.99, 
            xlabel=$x$, ylabel=$y$,grid=major 
            ] 
            \addplot [draw=none, pattern=north west lines, pattern color=blue!40, domain=0.01:6]
            {1/x} \drawge;
            \addplot[very thick, domain=0.01:6,-] {1/x}; 
            \addplot [draw=none, pattern=north west lines, pattern color=red!40, domain=-3:0]
            {-3:0} \drawge;
            \end{axis} 
        \end{tikzpicture} 
    %   \caption{Prípad 1} 
    \end{figure}    

\end{document}
  • @PhelypeOleinik can I ask, how did you change it, please? – Euler Nov 30 '18 at 22:19
  • 1
    You mean to format as code? You just select the code and click the {} button, like here: i.stack.imgur.com/EGnsG.png Any line that begins with four spaces is formatted as code. – Phelype Oleinik Nov 30 '18 at 22:24
8

You make your own life a bit complicated by running all these things in the beginning. Assuming you will use them somewhere, I left them in, and propose

\documentclass{article} 
\usepackage{pgfplots} 
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={ \pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style=opaque, grid=none}\pgfplotsdrawaxis}}}
\newcommand{\drawge}{-- (rel axis cs:5,0) -- (rel axis cs:5,5) -- (rel axis cs:0,5) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:5,5) -- (rel axis cs:5,0) -- (rel axis cs:0,0) \closedcycle}

\begin{document}

    \begin{figure}[htpb] 
        \centering 
        \begin{tikzpicture} 
            \begin{axis}[only axis on top,
            axis line style=very thick, 
            axis x line=middle, 
            axis y line=middle, 
            ymin=-3,ymax=5.99,xmin=-3,xmax=5.99, 
            xlabel=$x$, ylabel=$y$,grid=major,samples=101 
            ] 
            \addplot [draw=none, pattern=north west lines, pattern color=blue!40, domain=0.01:6]
            {1/x} \drawge;
            \addplot[very thick, domain=0.01:6,-] {1/x};
            \addplot[draw=none,name path=top] {6};
            \addplot[draw=none,name path=bottom] {-6};
            \addplot [pattern=north west lines, pattern color=red!40] fill between [of=top and bottom,soft  clip={domain=-3:0}];
            \end{axis} 
        \end{tikzpicture} 
    %   \caption{Prípad 1} 
    \end{figure}    

\end{document}

enter image description here

As for additional request.

\documentclass{article} 
\usepackage{pgfplots} 
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={ \pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style=opaque, grid=none}\pgfplotsdrawaxis}}}
\newcommand{\drawge}{-- (rel axis cs:5,0) -- (rel axis cs:5,5) -- (rel axis cs:0,5) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:5,5) -- (rel axis cs:5,0) -- (rel axis cs:0,0) \closedcycle}

\begin{document}

    \begin{figure}[htpb] 
        \centering 
        \begin{tikzpicture} 
            \begin{axis}[only axis on top,
            axis line style=very thick, 
            axis x line=middle, 
            axis y line=middle, 
            ymin=-3,ymax=5.99,xmin=-3,xmax=5.99, 
            xlabel=$x$, ylabel=$y$,grid=major,samples=101 
            ] 
%             \addplot [draw=none, pattern=north west lines, pattern color=blue!40, domain=0.01:6]
%             {1/x};
            \addplot[name path=A,very thick, domain=0.01:6,-] {1/x};
            \addplot[draw=none,name path=top, domain=-3:6] {6};
            \addplot[draw=none,name path=bottom] {-6};
            \addplot [pattern=north west lines, pattern color=red!40] fill between [of=top and bottom,soft  clip={domain=-3:0}];
            \addplot [pattern=north west lines, pattern color=blue!40] fill
            between [of=top and A,soft  clip={domain=0:6}];
            \end{axis} 
        \end{tikzpicture} 
    %   \caption{Prípad 1} 
    \end{figure}    

\end{document}

enter image description here

  • Thank you. Can I ask, how should I change it, if I want to have function x<=0 red? Because part for y>0 is violet. :( Maybe I have some mistake in functon y>= (1/x) for x>0? Is there some possibility to do it easier? – Euler Nov 30 '18 at 22:42
  • 1
    @Euler I added something... – user121799 Nov 30 '18 at 22:47

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.