# Tikz plotting 3d

I'm trying to make a 3d plot with tikz, but I was not able to do it. I always get a rubbish messy chaos of lines heading into all directions. I wanted to plot f(x,y) = x^3 / y^2. I was able to do it with gnuplot:

            \begin{gnuplot}
set terminal epslatex color
set size 1,1
set isosamples 100
unset key
set grid
set xrange [-5:5]
set yrange [-5:5]
set zrange [-5:5]
set hidden3d
set view 62, 308, 1, 1
splot x**3 / y**2 lt rgb "red"
\end{gnuplot}


The result looks like this:

This solution still has some problems. E.g. for the area around (0,0,0) there are very litte samples, so there is a lot of white space and the edges are spicky. My question: Is it even possibile to make this plot with tikz? Can the plot be smother and better looking for the area around (0,0,0)? If true, can you help me?

• Welcome to TeX.SX! Please don't post code fragments. Instead, put your fragments into a complete compilable document that shows the problem. – Alessandro Cuttin May 15 '20 at 16:24
• In addition to what @AlessandroCuttin is saying, you may want to define better what you want. There are different variants of 3d plot (e.g. surface or mesh and so on). Which of those are you after? – user194703 May 15 '20 at 17:06

\documentclass{article}
\begin{document}
<<echo=F,dev="tikz",message=FALSE>>=
s <- function (x, y) {return (x^3/y^2)}
x <- seq(-1, 1, length= 30)
y <- x
z <- outer(x, y, s)
library(lattice)
library(gridExtra)
trellis.par.set("axis.line", list(col=NA,lty=1,lwd=1))
col.regions=rainbow(150), ylim=c(12,18),
scales = list(arrows = FALSE),
colorkey=F)
@
\end{document}

• Is R included in LaTeX? – Titanlord May 16 '20 at 8:56
• @Titanlord R is independent statistical graphic language, but that can be integrated with LaTeX code as above. You should know also about the knitr R package that make this possible (the short way: install RStudio, save this as a new Sweave document and push the "Compile PDF" button). – Fran May 16 '20 at 22:08

Along the lines of my answer Plotting a 3d surface in tikz, with a limit to the infinity, I combine a computer algebra system, SAGE with LaTeX by way of the sagetex package. First, it helps to know what the plot will look like. Go to a Sage Cell Server and typing in the following lines:

var('y')
plot3d(x^3/y^2,(x,-2,2),(y,-2,2))


followed by enter and rotating around you'll see a picture like:

Now that you know how the surface should look we can see the problem is the values of height spike high in the positive and negative direction. Using a modification of the code posted in my answer referenced above we get:

\documentclass[11pt,border={10pt 10pt 10pt 10pt}]{standalone}
\usepackage{pgfplots}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
x = var('x')
y = var('y')
step = .25
x1 = -4
x2 = 4
y1 = -4
y2 = 4
output = ""
output += r"\begin{tikzpicture}[scale=1.0]"
output += r"\begin{axis}[view={-35}{45},xmin=%d, xmax=%d, ymin=%d, ymax=%d]"%(x1,x2,y1,y2-step)
# rows is the number of y values
for y in srange(y1,y2,step):
for x in srange(x1,x2,step):
if (x^3/y^2)<200 and (x^3/y^2)>-200:
output += r"(%f, %f, %f) "%(x,y,x^3/y^2)
elif (x^3/y^2)<=-10:
output += r"(%f, %f, %f) "%(x,y,-200)
else:
output += r"(%f, %f, %f) "%(x,y,200)
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}


The code replaces every height at least 200 by 20 and every height less than -200 with -200. This gives a cap to the plot. Changing the viewing angle view={-35}{45} to something which illustrates the behavior of the plot better gives us this:

Without using Sage, you have more work to do each time to prevent the edges from being jagged. Sometimes that's easier than others; the accepted answer converted to polar. A recent question had another solution. SAGE is not part of LaTeX so you either have to install it on your computer or you access it through a free Cocalc account. If you're plotting surfaces like this, that might be a good tool to work with. The documentation for sagetex is here on CTAN.

• TBH. I tried Sagetex, and it made pretty impressive plots, but i was not able to use it with latex. That's y i searched for a tikz solution. – Titanlord May 16 '20 at 8:57
• There are several examples of sagetex with/without tikz on this site. Riemann Zeta, Weierstrass, Fourier. If you use Cocalc, just copy/paste and change the function. – DJP May 16 '20 at 21:33