Is there a way to draw primitives in 3D with Python? - python

I want to draw 3D primitives like spheres, cylinders and planes (patches) in a 3D plot and I would like to be able to interactively rotate, translate and zoom the scene. I want to do that in Python. I'm use to use Matplotlib for 2d graphs but I never worked with 3D graphics with Python.
Any suggestions?
Any link to tutorials?
Any ideas?

If you're used to matplotlib, then mplot3d is probably a good option if it meets your requirements.
Alternatively there is VPython. This allows you greater freedom to create arbitrary objects and manipulate them, but, of course, more to learn.

Related

Matplotlib alternative for 3D scatter plots

I am having a hard time using Matplotlib to visualize reprojection results of my data in 3 dimensions after applying Principle components analysis or Linear discriminant analysis. After doing a scatter plot, I cannot rotate the data or change the point of view while zooming easily (Rotation axis stays the same even after you zoom, and if you zoom too much points just disappear) and every change takes one second to occur. Matplotlib is very useful but for this specific use case it starts to get very frustrating as it probably wasn't designed for such tasks. Is there an alternative to Matplotlib in Python that can handle 3d scatter plots better and where one could fluidly navigate through the cloud?
An example is shown in the next figure. I have drawn spheres around each data cluster corresponding to a specific class and colored overlapping spheres with red. Now I want to see how these sphere intersect. I think the biggest problem with Matplotlib is that it doesn't allow shifting of the whole graph with the mouse, it only allows rotation around a fixed point, which makes things very messy once you zoom a bit.
matplotlib is not quite mature for 3d graphics :
http://matplotlib.org/mpl_toolkits/mplot3d/faq.html
mplot3d was intended to allow users to create simple 3D graphs with the same “look-and-feel” as matplotlib’s 2D plots. Furthermore, users can use the same toolkit that they are already familiar with to generate both their 2D and 3D plots.
I don't think easy navigation in a 3d plot is easily doable (even 3d scaling is not possible without tweaking the lib). mplot3d was not really intended to be a full-fledged 3D graphics library in the beginning, but more a nice addition for people who needed basic 3D and who were acquainted with matplotlib 2D plot structure.
You might want to take a look at MayaVI (which is pretty good) :
MayaVi2 is a very powerful and featureful 3D graphing library. For advanced 3D scenes and excellent rendering capabilities, it is highly recomended to use MayaVi2.
Note that unlike matplotlib, MayaVI is not yet compatible with Python3 (and might not be in the foreseeable future), so you'll need a Python2 installation.
A very good alternative, but not in Python, is the 3D plot from ILNumerics (http://ilnumerics.net/). It is in .NET
Matplotlib works alright for 3D however, not too fast when interactivity is needed:
https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
Mayavi is really fast and compatible with Python 3:
https://docs.enthought.com/mayavi/mayavi/mlab.html#id1

Density plots from matlab to matplotlib

Since some years ago I use matlab for my plots (mostly density plots), but now I want to change to matplotlib. I have a problem trying to figure out how to get analogous plots in matplotlib. I have to represent a 2D array. In matlab I used to use the surf function, and then change to view(2) (az=0 and el=90). An example:
surf(X,Y,log10(z),'FaceColor','interp','EdgeColor','none')
view(2)
In matplotlib I have tried some functions, but I have not got the same feeling. m3plot is a computationally expensive toolkit and it is not the same as using surf. imshow does not allow to use log functions in his arguments (like the example), and log values is something mandatory for me. Then it is pcolor, but I can not find a 'FaceColor'-like option to smooth the edges. I would like to know if someone knows what is the best equivalent in matplotlib.
Thank you for your time!
Try installing mayavi which has the surf function (mayavi is a fully-blown 3D visualisation library using hardware acceleration)
Finally, the solution that suits me is to use the routine pcolormesh(). This combined with the option shading='gouraud' interpolates the data and smooth the edges. In addition, it works pretty well with large arrays in comparision with pcolor.

Mapping a texture to a mayavi mesh

I'm using mayavi in python to visualize some data. I have a surface in 3D space and I want to map an image to it. I have looked at mayavi documentation and all I could find was this, which really doesn't help me much. Has anybody done something like this before?
I found that the mesh function actually does take a scalars argument, which then colors the corresponding point on the surface in accordance to the chosen colormap. I still however don't know how to map an arbitrary RGB image to the surface.
I guess you didn't saw the chapter "Python scripting for 3D plotting". There is a collection of the mayavi commands which can be used inside python. For example the section "3D Plotting functions for numpy arrays". If you follow the links you find some description about the functions and a small example for each of them.
For your case you should take mesh or surf. Both functions are kind of equal, but are different. For example the surf function is more a subfunction of mesh, which is my opinion. You don't really have to define x and y-coordinates, you just need the z-coordinates in a 2D array. If you want to use mesh you have to pass all three coordinates (x,y,z) as 2D arrays.
I would suggest to check the examples.
Is this an answer to your question? Maybe you should be more specific about what you want to do.
Have a nice day and Happy Holidays.

3D plotting in Python

What are my options for 3D plotting in Python?
It also must be redistributable.
I assume you're asking for reccomendations, rather than just what modules are available...
Personally, I mostly use Mayavi/Mlab/TVTK. It's essentially a higher level interface to VTK. It's available under a BSD-style license (If I recall correctly...), so it should be freely re-distributable. If you're visualizing scientific data, it's an excellent choice.
For simpler 3D plots, matplotlib's Axes3D is quite nice. Take a look at the 3D examples (towards the bottom) on the matplotlib gallery page. However, it's not well suited to visualize volumetric data or more complex surfaces.
Naturally, those aren't the only options, but they're the two I would reccomend.
If you want to drop down to a lower level, there's always PyOpenGL, too.
At any rate, hopefully that points you in the right direction...
One more variant is Python interface to MathGL. This is GPL library with large set of 2D and 3D plotting graphics types.

3D Polygons in Python

As far as I am aware there is no inbuilt polygon functionality for Python. I want to create a 3D map and figured that polygons would be the best way to go about it.
Not wanting to reinvent the wheel I did some googling and found that there's a lot of Python stuff out there, but I couldn't find what I wanted. Thus before I reinvent the wheel (or invent it a whole), does anybody know of a Polygon system for Python?
Note that it does need to be 3D (I found quite a few 2D ones). Note also that I am not interested in the displaying of them but in storing them and the datastructure within Python.
Thanks
One of the most complete geography/mapping systems available for Python that I know about is GeoDjango. This works on top of the Django, an MVC framework. With it comes a large collection of polygon, line and distance calculation tools that can even take into account the curvature of the earth's surface if need be.
With that said, the quickest way I can think of to produce a 3D map is using a height map. Create a two dimensional list of tuples containing (x, y, z) coordinates. Each tuple represents an evenly spaced point on a grid, mapped out by the dimensions of the array. This creates a simple plane along the X and Z axes; the ground plane. The polygons that make up the plane are quads, a polygon with four sides.
Next, to produce the three dimensional height, simply give each point a Y value. This will create peaks and valleys in your ground plane.
How you render this will be up to you, and converting your grid of points into a polygon format that something like OpenGL can understand may take some work, but have a look at Visual Python, its the simplest 3D library I've seen for Python.
I think you mean Polyhedron, not Polygon .. and you might wanna look at vpython
CGAL is a C++ geometry library which, amongst other things, models Polyhedra (3D flat-surfaced shapes)
It has Python bindings available. The documentation for the Polygon3 class is here:
http://cgal-python.gforge.inria.fr/Manual/CGAL.Polyhedron.html#Polyhedron_3

Categories

Resources