Hi,

I'm trying to plot an outline of an arbitrary 3D shape using matplotlib's plot_surface, and I wanted to ask if any one has any ideas as to how to do it. Here's the beginnings of a simple example:

# create a grid

resolution = 10

xs = np.linspace(-1,1,resolution)

ys = np.linspace(-1,1,resolution)

zs = np.linspace(-1,1,resolution)

X,Y,Z = np.meshgrid(xs, ys, zs)

#Then we can calculate the square of the distance of each point to the center:

W = X**2 + Y**2 + Z**2

# Let's see which points are within a certain radius:

C = W < 1.

# this can be used to create a point-cloud

x = X[C]

y = Y[C]

z = Z[C]

From this I tried a lot options, the best being to do some XOR operations on the array C to get a list of points which are on the surface of the sphere. My question is, can anybody see a reasonable way to plot this surface using mplot3D?

Of course plotting a sphere can be done using spherical coordinates, but that defeats the purpose of this example This is just a simplification as the application is actually to draw 3D contours (i.e. shapes, rather than iso-lines) of a three-parameter probability distribution.

Thanks in advance,

-Peter

Generally speaking, a plottable 3D surface can be represented parametrically in 2D (hence why it is a surface). Your point cloud can not be represented parametrically in 2 dimensions, hence why you are having difficulty figuring out how to plot it as a surface. I used to have similar problems with 3D plotting (both here and in Matlab) before I came to this realization.

Your comment “of course, plotting a sphere can be done in spherical coordinates” is actually the right thought process. Spherical coordinates is how you parametrize your spherical surface. Pick a coordinate system that is relevant to your problem at hand and use it.

I hope this helps!

Ben Root

## ···

On Sat, Nov 1, 2014 at 2:07 PM, Peter Kerpedjiev <pkerpedjiev@…287…> wrote:

Hi,

I’m trying to plot an outline of an arbitrary 3D shape using

matplotlib’s plot_surface, and I wanted to ask if any one has any ideas

as to how to do it. Here’s the beginnings of a simple example:

# create a grid

resolution = 10

xs = np.linspace(-1,1,resolution)

ys = np.linspace(-1,1,resolution)

zs = np.linspace(-1,1,resolution)

X,Y,Z = np.meshgrid(xs, ys, zs)

#Then we can calculate the square of the distance of each point to the

center:

W = X**2 + Y**2 + Z**2

# Let’s see which points are within a certain radius:

C = W < 1.

# this can be used to create a point-cloud

x = X[C]

y = Y[C]

z = Z[C]

From this I tried a lot options, the best being to do some XOR

operations on the array C to get a list of points which are on the

surface of the sphere. My question is, can anybody see a reasonable way

to plot this surface using mplot3D?

Of course plotting a sphere can be done using spherical coordinates, but

that defeats the purpose of this example This is just a

simplification as the application is actually to draw 3D contours (i.e.

shapes, rather than iso-lines) of a three-parameter probability

distribution.

Thanks in advance,

-Peter

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Sorry Ben, but this is not an answer. P.K. clearly states that his

case is more complicated, and no parametrization is likely. Anyway,

the spherical exercise as it is presented uses the 3D constraint, it

is not parametric.

The general solution is the , which is a well established technology (although

non-trivial). For example the algorithms and their variants.

These are techniques for the polygonization of a mesh.

If P.K. has an analytic formula for his distributions, and is able

to compute gradients, etc., there are some more efficient

techniques, but in general it is the case for solving the equation

F(x,y,z)=0 for {x,y,z} ; here Matplotlib doesn’t offer (yet) any

tools if I am not mistaken.

Jerzy Karczmarczuk

Caen, France.

## ···

Le 01/11/2014 19:21, Benjamin Root

answers the query of Peter Kerpedjiev, who wants to plot (with

Matplotlib) the surface of an implicit surface (at least it was

his presented example).

```
Your comment "of course, plotting a sphere can be done
```

in spherical coordinates" is actually the right thought process.

Spherical coordinates is how you parametrize your spherical

surface. Pick a coordinate system that is relevant to your problem

at hand and use it.

** polygonization of the implicit

surface*** marching cubes / marching

simplices*

Jerzy,

Actually, my response is still completely valid. You can only plot surfaces that can be represented parametrically in two dimensions. Find me a single plotting library that can do differently without having to get to this final step. For matplotlib, it is up to the user to get the data to that point. As you stated, he is seeking polygonization of an *implicit* surface. Matplotlib has no means of understanding this. And this is unlikely to happen anytime soon given the inherent 2D limitations of Matplotlib.

I am sorry if the answer is unsatisfactory to you, but it is the correct one to give.

Ben Root

## ···

On Sat, Nov 1, 2014 at 2:49 PM, Jerzy Karczmarczuk <jerzy.karczmarczuk@…3937…> wrote:

```
Le 01/11/2014 19:21, Benjamin Root
```

answers the query of Peter Kerpedjiev, who wants to plot (with

Matplotlib) the surface of an implicit surface (at least it was

his presented example).

```
Your comment "of course, plotting a sphere can be done
```

in spherical coordinates" is actually the right thought process.

Spherical coordinates is how you parametrize your spherical

surface. Pick a coordinate system that is relevant to your problem

at hand and use it.

```
Sorry Ben, but this is not an answer. P.K. clearly states that his
```

case is more complicated, and no parametrization is likely. Anyway,

the spherical exercise as it is presented uses the 3D constraint, it

is not parametric.

```
The general solution is the ** polygonization of the implicit
```

surface** , which is a well established technology (although

non-trivial). For example the * marching cubes / marching

simplices* algorithms and their variants.

```
These are techniques for the polygonization of a mesh.
If P.K. has an analytic formula for his distributions, and is able
```

to compute gradients, etc., there are some more efficient

techniques, but in general it is the case for solving the equation

F(x,y,z)=0 for {x,y,z} ; here Matplotlib doesn’t offer (yet) any

tools if I am not mistaken.

```
Jerzy Karczmarczuk
Caen, France.
```

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Jerzy and Ben,

```
Thanks for you answers!
I must say that although Ben is right in principle, Jerzy's answer
```

is exactly what I was looking for. Even if matplotlib can’t do it by

itself, there appears to be other libraries that do the heavy

lifting and return a set of triangles which can then be placed in a

Polygon3DCollection and plotted.

```
It's always good to know what something is called. Searching for '3D
```

contours’ was leading nowhere, but plugging in ‘** polygonization of

the implicit surface’** returned a multitude of descriptions of

the problem and libraries. It turns out I had been trying to

implement the marching cubes algorithm myself for the better part of

the last week. Oops!

```
Thanks again to the both of you!!
-Peter
```

## ···

On 11/1/14, 8:34 PM, Benjamin Root

wrote:

Jerzy,

```
Actually, my response is still completely valid. You can
```

only plot surfaces that can be represented parametrically in

two dimensions. Find me a single plotting library that can

do differently without having to get to this final step. For

matplotlib, it is up to the user to get the data to that

point. As you stated, he is seeking polygonization of an

*implicit* surface. Matplotlib has no means of understanding

this. And this is unlikely to happen anytime soon given the

inherent 2D limitations of Matplotlib.

```
I am sorry if the answer is unsatisfactory to you, but it is
```

the correct one to give.

Ben Root

```
On Sat, Nov 1, 2014 at 2:49 PM, Jerzy
```

Karczmarczuk <jerzy.karczmarczuk@…3937…>

wrote:

```
Le 01/11/2014 19:21, Benjamin Root answers the query
```

of Peter Kerpedjiev, who wants to plot (with Matplotlib)

the surface of an implicit surface (at least it was his

presented example).

```
Your comment "of course,
```

plotting a sphere can be done in spherical

coordinates" is actually the right thought process.

Spherical coordinates is how you parametrize your

spherical surface. Pick a coordinate system that is

relevant to your problem at hand and use it.

```
Sorry Ben, but this is not an answer. P.K. clearly
```

states that his case is more complicated, and no

parametrization is likely. Anyway, the spherical exercise

as it is presented uses the 3D constraint, it is not

parametric.

```
The general solution is the ** polygonization of the
```

implicit surface** , which is a well established

technology (although non-trivial). For example the * marching

cubes / marching simplices* algorithms and their

variants.

```
These are techniques for the polygonization of a mesh.
If P.K. has an analytic formula for his distributions, and
```

is able to compute gradients, etc., there are some more

efficient techniques, but in general it is the case for

solving the equation F(x,y,z)=0 for {x,y,z} ; here

Matplotlib doesn’t offer (yet) any tools if I am not

mistaken.

```
Jerzy Karczmarczuk
Caen, France.
```

```
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
[https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)
```

```
------------------------------------------------------------------------------
```

```
_______________________________________________
Matplotlib-users mailing list
```

Matplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-users

Jerzy,

I really do not wish to get into an argument with you. This seems to happen every time you come onto this mailing list. If “winning” this argument is so important to you, then you may have it. I will not continue to split hairs with you.

Thank you for mentioning the concept of implicit surfaces and marching cubes. I am glad that is helpful to the original poster.

Cheers!

Ben Root

## ···

On Sat, Nov 1, 2014 at 4:38 PM, Jerzy Karczmarczuk <jerzy.karczmarczuk@…3937…> wrote:

```
Le 01/11/2014 20:34, Benjamin Root a
```

écrit :

```
Actually, my response is still completely valid. You can
```

only plot surfaces that can be represented parametrically in

two dimensions. Find me a single plotting library that can

do differently without having to get to this final step.

```
1. I did not claim that you said something invalid, only that it
```

seemed weakly appropriate for Peter Karpedjiev.

```
2. Unfortunately NOW you say something inexact. Of course you can
```

plot implicit surfaces without 2D parametrization. All the

ray-tracing technology is adapted to that.

```
POV-Ray, etc., if you want some names. Some terrain renderers, such
```

as Terragen use it as well. YafaRay was once embeddable in Blender.

```
I am sorry if the answer is unsatisfactory to you, but it
```

is the correct one to give.

```
3. But "my satisfaction" is not the issue. I tried to direct Peter
```

in some **usable** direction, according to my experience.

```
Since the implementation of marching cubes, and other similar
```

techniques is awkward, not very efficient in Python (I tried it), I

suggest very strongly that Peter direct himself -at least

temporarily - to the RT methods.

```
If he wishes, I might help him in private. Writing a Ray Tracer in
```

Python/numpy is not so difficult (I gave it as student projects a

few times), and of course everything functional may be programmed in

POV-Ray.

```
Best regards.
Jerzy K.
```

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users