3

I am drawing a surf 3d plot in Tikz/Pgf using gnuplot. This surface need to be projected on a plane, which can be achieved by adding another surf plot.

The thing is that the transition between colors, in both surf plots actually is not very smooth, despite using

shader=interp

One possibility is to increase the number of samples however building becomes slow and I cannot exceed 75 samples.

An example code can be found right next

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{patchplots}


\begin{document}

\begin{tikzpicture}
    \begin{axis}    [width=\textwidth,
                     height=\textwidth,
                     ultra thick,
                     colorbar,
                     colorbar style={yticklabel style={text width=2.5em,
                                                      align=right,
                                                  /pgf/number format/.cd,
                                                   fixed,
                                                   fixed zerofill,
                                                   precision=1,
                                                   },
                                    },
                     xlabel={$\rho_x=k_xr_x$},
                     ylabel={$\rho_y=k_yr_y$},
                     zlabel={$j_l(\rho)$},
                     3d box,
                     zmax=2.5,
                     xmin=-3, xmax=3,
                 ymin=-3.1, ymax=3.1,
                 ytick={-3, -2, ..., 3},
                 grid=major,
                     grid style={line width=.1pt, draw=gray!30, dashed},
                     x tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    },
                y tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    },
                z tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    },
                    ]
        \addplot3[surf, 
                      shader=interp,
                      mesh/ordering=y varies,
                      domain=-3:3,
                      y domain=-3.1:3.1,
                      ]
             gnuplot {besj0(x**2+y**2)};

         \addplot3[surf,
                  samples=51,
                  shader=interp,
                  mesh/ordering=y varies,
                  domain=-3:3,
                  y domain=-3.1:3.1,
                  point meta=rawz,
                  z filter/.code={\def\pgfmathresult{2.5}},
                  ]
             gnuplot {besj0(x**2+y**2)};
    \end{axis}
\end{tikzpicture}

\end{document}

and the result of this code is the following image

enter image description here

Any idea on how to make a smoother transition from color to color?

  • 1
    With pleasure! No problem! – Thanos Feb 24 at 21:00
4

If your main concern is the color transitions, then you may want to use a polar plot because the function only depends on the radius and not on the angle. Then you could increase the samples in radial direction while leaving the samples in angular direction comparatively small.

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

\begin{document}  
\begin{tikzpicture}
    \begin{axis}    [width=\textwidth,
                     height=\textwidth,
                     ultra thick,
                     colorbar,
                     colorbar style={yticklabel style={text width=2.5em,
                                                      align=right,
                                                  /pgf/number format/.cd,
                                                   fixed,
                                                   fixed zerofill,
                                                   precision=1,
                                                   },
                                    },
                     xlabel={$\rho_x=k_xr_x$},
                     ylabel={$\rho_y=k_yr_y$},
                     zlabel={$j_l(\rho)$},
                     3d box,
                     zmax=2.5,
                     xmin=-3, xmax=3,
                     ymin=-3.1, ymax=3.1,
                     ytick={-3, -2, ..., 3},
                     grid=major,
                     grid style={line width=.1pt, draw=gray!30, dashed},
                     x tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    },
                     y tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    },
                     z tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    }, 
                    data cs=polar,
                    ]
        \addplot3[surf, samples=37,samples y=101,
                      shader=interp,
                      z buffer=sort,
                      %mesh/ordering=y varies,
                      domain=0:360,
                      y domain=3.1:0,
                      ]
             gnuplot {besj0(y**2)};

        \addplot3[surf, samples=36, samples y=101,
                      shader=interp,
                      %mesh/ordering=y varies,
                      domain=0:360,
                      y domain=0:3.1,
                      point meta=rawz,
                      z filter/.code={\def\pgfmathresult{2.5}},
                      ]
             gnuplot {besj0(y**2)};


    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

As a "side-effect" the wiggles will also disappear as they result from plotting a rotationally symmetric function in cartesian coordinates.

And here is a combination of a cartesian and a polar plot.

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

\begin{document}  
\begin{tikzpicture}
    \begin{axis}    [width=\textwidth,
                     height=\textwidth,
                     ultra thick,
                     colorbar,
                     colorbar style={yticklabel style={text width=2.5em,
                                                      align=right,
                                                  /pgf/number format/.cd,
                                                   fixed,
                                                   fixed zerofill,
                                                   precision=1,
                                                   },
                                    },
                     xlabel={$\rho_x=k_xr_x$},
                     ylabel={$\rho_y=k_yr_y$},
                     zlabel={$j_l(\rho)$},
                     3d box,
                     zmax=2.5,
                     xmin=-3, xmax=3,
                     ymin=-3.1, ymax=3.1,
                     ytick={-3, -2, ..., 3},
                     grid=major,
                     grid style={line width=.1pt, draw=gray!30, dashed},
                     x tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    },
                     y tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    },
                     z tick label style={/pgf/number format/.cd,
                                            fixed,
                                            fixed zerofill,
                                            precision=1
                                    }, 
                    ]
        \addplot3[surf, samples=75,
                      shader=interp,
                      mesh/ordering=y varies,
                      domain=-3:3,
                      y domain=-3.1:3.1,
                      ]
             gnuplot {besj0(x**2+y**2)};
        \addplot3[surf, samples=36, samples y=101,
                      shader=interp,
                      %mesh/ordering=y varies,
                      domain=0:360,
                      y domain=0:3.1,
                      point meta=rawz,
                      data cs=polar,
                      z filter/.code={\def\pgfmathresult{2.5}},
                      ]
             gnuplot {besj0(y**2)};


    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Thank you very much for your answer! The point is that in the 3d surface the folding of the function is more prominent, therefore the wiggles are indeed needed! I could however use a polar plot on the projection. Is this possible? – Thanos Feb 24 at 18:58
  • @Thanos Yes, but I do not understand what you mean by "folding". – user121799 Feb 24 at 19:00
  • I mean the wiggles you mentioned in the side-effect. – Thanos Feb 24 at 19:00
  • @Thanos But aren't the wiggles "unphysical", meaning that the true Bessel function doesn't have them (since they imply an angular dependence, which J0 does not have)? – user121799 Feb 24 at 19:02
  • @ marmot You are perfectly right. However, I believe that for illustration reasons, someone can better observe the oscillating behaviour. – Thanos Feb 24 at 19:07

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.