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:

                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"

The result looks like this:

enter image description here

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?

  • 1
    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 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 at 17:06

With R instead of gnuplot:


s <- function (x, y) {return (x^3/y^2)}
x <- seq(-1, 1, length= 30)
y <- x
z <- outer(x, y, s)
trellis.par.set("axis.line", list(col=NA,lty=1,lwd=1))
wireframe(z, drape=T, shade=T, xlab="x", ylab="y", 
col.regions=rainbow(150), ylim=c(12,18), 
scales = list(arrows = FALSE),  
| improve this answer | |
  • Is R included in LaTeX? – Titanlord May 16 at 8:56
  • 1
    @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 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:


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

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}
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)
output += r"\addplot3[surf,mesh/rows=%d] coordinates {"%((y2-step-y1)/step+1)
# 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)
            output += r"(%f, %f, %f) "%(x,y,200)
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"

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: enter image description here

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.

| improve this answer | |
  • 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 at 8:57
  • 1
    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 at 21:33

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.