Graphics, Figures & TablesShaded ellipsoid with PGFplots

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Tyrion1989
Posts: 2
Joined: Thu Aug 13, 2015 1:05 pm

Shaded ellipsoid with PGFplots

Post by Tyrion1989 »

Hey, I'm trying to display a shaded 3D ellipsoid with axis labels with PGFplots. So far I achieved this.

Code: Select all

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots,tikz-3dplot}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
    	width=0.8\textwidth,
        axis equal,
        axis lines = center,
        y label style={at={(axis cs:0,2,0)},anchor=west},
        xlabel = {$\lambda_1,\ \vec{e}_1$},
        ylabel = {$\lambda_2,\ \vec{e}_2$},
        zlabel = {$\lambda_3,\ \vec{e}_3$},
        ticks=none,
    ]
    \addplot3[%
    	shader=interp,
        opacity = 0.3,
        fill opacity=0.3,
        surf,
        colormap/blackwhite,
        variable = \u,
        variable y = \v,
        domain = 0:180,
        y domain = 0:360,
    ]
    ({2*cos(u)*sin(v)}, {2*sin(u)*sin(v)}, {0.5*cos(v)});
    \end{axis}
\end{tikzpicture}
\end{document}
The z label is at the right position in my PDF document, I dont't know why it is deplaced here.
This shading does not give a 3D impression, I would like to have somethin like in the attached file. Is it possible to define a new 3d shading in this coordinate system? I could not find such a command in the pgfplot documentation. All predefined shadings are not suitable.
Glad for any help!
Attachments
FA_tensor.png
FA_tensor.png (52.96 KiB) Viewed 5329 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Shaded ellipsoid with PGFplots

Post by Stefan Kottwitz »

Welcome to the forum!

You could use point meta = u. And you could play with the colormap, as I did on the axis.

I added colormap={}{ gray(0cm)=(0.8); gray(1cm)=(0);} and point meta = u to the plot:
ellipsoid.png
ellipsoid.png (20.53 KiB) Viewed 5312 times
edit: above was rendered from PDF to PNG by GIMP, seems to become too dark, here's how my previewer and Overleaf render it:
ellipsoid-lighter.png
ellipsoid-lighter.png (28.65 KiB) Viewed 5307 times
Full code:

Code: Select all

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots,tikz-3dplot}
\makeatother
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
        width=0.8\textwidth,
        axis equal,
        axis lines = center,
        y label style={at={(axis cs:0,2,0)},anchor=west},
        xlabel = {$\lambda_1,\ \vec{e}_1$},
        ylabel = {$\lambda_2,\ \vec{e}_2$},
        zlabel = {$\lambda_3,\ \vec{e}_3$},
        ticks=none,
        colormap={}{ gray(0cm)=(0.8); gray(1cm)=(0);}
    ]
    \addplot3[%
        shader=interp,
        opacity = 0.3,
        fill opacity=0.3,
        surf,
        variable = \u,
        variable y = \v,
        domain = 0:180,
        y domain = 0:360,
        point meta=u,
        samples = 50,
    ]
    ({2*cos(u)*sin(v)}, {2*sin(u)*sin(v)}, {0.5*cos(v)});
    \end{axis}
\end{tikzpicture}
\end{document}
Stefan
LaTeX.org admin
Tyrion1989
Posts: 2
Joined: Thu Aug 13, 2015 1:05 pm

Re: Shaded ellipsoid with PGFplots

Post by Tyrion1989 »

Hi, the problem is that the shading coordinate system is still wrong. I want the shading to look like if a light bulb would shine on the ellipsoid. So the colormap should extend from the top maybe and extend in all direction isotropically. Right now there is this "line" dividing the ellipsoid into two halfs. The grey parts on the left side of the upper half should be right so the shading has to be flipped at the y axis for the upper half.

I will try to seperate the ellipsoid into two plots, just have to change the variable domains.

Thanks!
Post Reply