1

I want to plot sqrt(-xy) if xy<=0, and -x^2+y if xy>0.

Please consider this MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\pgfplotsset{soldot/.style={color=black,only marks,mark=*}}
\pgfplotsset{holdot/.style={color=red,fill=white,very thick,only marks,mark=*}}

\begin{document}

\begin{center}                                                      
\begin{tikzpicture}[scale=2]
    \begin{axis}[
        legend pos=outer north east,
        axis lines = center,
        view={-35}{15},
        xticklabel style = {font=\tiny},
        yticklabel style = {font=\tiny},
        zticklabel style = {font=\tiny},
        xlabel = $x$,
        ylabel = $y$,
        zlabel = $z$,
        legend style={cells={align=left}}
    ]
    \addplot3[surf,mesh/ordering=y varies,shader=interp,domain=-3:0,domain y=0:3,samples=40, samples y=20] {sqrt(-x*y)};
    \addplot3[surf,mesh/ordering=y varies,shader=interp,domain=0:3,domain y=0:-3,samples=40, samples y=20] {sqrt(-x*y)};
    \addplot3[surf,mesh/ordering=y varies,shader=interp,domain=0:3,domain y=0:3,samples=40, samples y=20] {-x*x+y};
    \addplot3[surf,mesh/ordering=y varies,shader=interp,domain=-3:0,domain y=0:-3,samples=40, samples y=20] {-x*x+y};
    \end{axis}
\end{tikzpicture}
\end{center}

\end{document}

Actual graph

I cannot find a good view for display the function. I think I like the view from the x<0 and y<0 but it is the image and it is ugly. What do you recommend not using gnuplot?

Thanks!

EDIT. I also would like to graph 7 vectors with norm 1: 4 in the xy-plane, and 3 in the direction of x>0 and y>0. i.e.:

Vectors with norm 1

in 3D. If it is complicated to see, you can do it in 2D, like the image.

  • Probably we should do something with the graph at x>0 and y>0 (where the function is -x^2+y) because it is the ugliest part. – manooooh Oct 26 '18 at 23:28
  • 1
    I guess you could improve it somewhat by drawing the plots in a different order: first the plot on the right back, then left back, then right front and finally left front. But it is also a strange function that has jumps. You are sure you want this function? – user121799 Oct 27 '18 at 0:58
  • @marmot I want this function. That's a bit better: imgur.com/a/YgRwgwY, anyway I would like to fill the "holes" (and draw the vectors with your help). – manooooh Oct 27 '18 at 1:01
  • Are these two separate graphs or do you want to add the arrows on the first graph? And what do you mean by "fill the holes"? Do you want to plot just one function? – user121799 Oct 27 '18 at 1:14
  • @marmot if possible, I want to graph the 7 vectors with norm 1 in 3D. That's probably bad (very bad), so the vectors in 2D. This is a piecewise function, so all the four graphs are one function (in one graph). The "holes" are that spaces where the function is not plotted. – manooooh Oct 27 '18 at 1:19
2

Here's a proposal. Draw the axes by hand and hide what you have to hide.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[scale=2,
declare function={f(\x,\y)=ifthenelse(\x*\y<0,sqrt(-\x*\y),-\x*\x+\y);}]
    \begin{axis}[hide axis,
        legend pos=outer north east,
        view={75}{45},clip=false
    ]
     \draw[thick,-stealth] (0,0,0) -- (0,4,0) node[right]{$y$};
     \addplot3[opacity=0.8,surf,mesh/ordering=y varies,shader=interp,domain=-3:3,domain
     y=-3:3,samples=61, samples y=61,on layer=axis background] {f(x,y)};
     \addlegendentry{$f(x,y)$}
     \draw[thick,-stealth] (0,0,0) -- (0,0,4) node[right]{$z$};
     \draw[thick,-stealth] (0,0,0) -- (4,0,0) node[right]{$x$};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Any attempt to draw these unit vectors in the positive x-y plane does IMHO not look good. How about drawing them in the negative x-y plane?

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[scale=2,
declare function={f(\x,\y)=ifthenelse(\x*\y<0,sqrt(-\x*\y),-\x*\x+\y);}]
    \begin{axis}[hide axis,
        legend pos=outer north east,
        view={-145}{45},clip=false
    ]
     \addplot3[opacity=0.8,surf,mesh/ordering=y varies,shader=interp,domain=-3:3,domain
     y=-3:3,samples=61, samples y=61,on layer=axis background] {f(x,y)};
     \addlegendentry{$f(x,y)$}
     \draw[thick,-stealth] (0,0,0) -- (0,4,0) node[below]{$y$};
     \draw[thick,-stealth] (0,0,0) -- (0,0,4) node[right]{$z$};
     \draw[thick,-stealth] (0,0,0) -- (4,0,0) node[above]{$x$};
     \pgfplotsinvokeforeach{0,22.5,...,90}{%
     \draw[thick,-stealth] (0,0,0) -- ({-cos(#1)},{-sin(#1)},0);}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Here's a third version with ticks.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[scale=2,
declare function={f(\x,\y)=ifthenelse(\x*\y<0,sqrt(-\x*\y),-\x*\x+\y);}]
    \begin{axis}[axis on top,xlabel={$x$},ylabel={$y$},
        axis lines=middle,xtick={-1,0,1},ytick={-1,0,1},
        legend pos=outer north east,
          z axis line style={opacity=0},
        view={-145}{45},clip=false
    ]
     \addplot3[opacity=0.8,surf,mesh/ordering=y varies,shader=interp,domain=-3:3,domain
     y=-3:3,samples=61, samples y=61,on layer=axis background] {f(x,y)};
     \addlegendentry{$f(x,y)$}
     \draw[-stealth] (0,0,0) -- (0,0,4) node[right]{$z$};
     \pgfplotsinvokeforeach{0,22.5,...,90}{%
     \draw[-stealth] (0,0,0) -- ({-cos(#1)},{-sin(#1)},0);}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Great! If the graph + vectors is ugly then f(x,y) in one graph (like your MWE), and vectors in another one (probably view={0}{90}). If you find an excellent view where the graph + vectors looks nice, then one graph. :) – manooooh Oct 27 '18 at 1:26
  • The second part of your question I still don't understand. Do you want to draw these vectors in this plot or in a separate plot? hmmm... now your plot is much better, but I think that the only vector which we could not see will be v=(0,-1). Can you find a better view where we could see the 7 vectors with norm 1? If "no" then plots it in a new graph with view={0}{90} (if you want). – manooooh Oct 27 '18 at 7:17
  • 1
    @manooooh Thanks for your edit here. I have a new proposal. What do you think? (I will go cycling now but will be back in a few hours.) – user121799 Oct 27 '18 at 19:14
  • You're welcome and awesome!! I only did a few changes: view={-145}{45} -> view={145}{45}; added the vectors (-1,0) and (0,-1) (and changed the direction from \draw[thick,-stealth] (0,0,0) -- ({-cos(#1)},{-sin(#1)},0); to \draw[blue,thick,-stealth] (0,0,0) -- ({cos(#1)},{sin(#1)},0);); and delete opacity=0.8. Very easy code, thanks!! – manooooh Oct 27 '18 at 20:18
  • 1
    @manooooh If you hide only the z axis and draw it by hand, you can add ticks. – user121799 Oct 27 '18 at 22:51

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.