4

I have just started to learn python and I encounter a problem while trying to produce a figure.

I have a large set of points (~ 42000) with X-Y-Z coordinates and with several variable associated (temperature, water content ...) I would like to plot all this stuff in one graph but it appears to be impossible with my level of python knowledge.. All these points are situated on a cartesian regular grid. So I wanted to produce a meshgrid grid with numpy but the I'm stuck .. Basically I want to transfrom 1D vector (X,Y,Z and T let's say) into a 3d grid with interpolated data. Is that possible ?

Could you please help me ?

user3473016
  • 57
  • 1
  • 1
  • 8
  • You can use mayavi probably. Something similar to this `from mayavi import mlab pts = mlab.points3d(x, y, z, s)` . More information is given here http://docs.enthought.com/mayavi/mayavi/mlab.html – chase Mar 28 '14 at 16:07
  • 1
    This sounds like a VTK problem. Use mayavi or ParaView. mayavi has a GUI and Python scripting frontend to VTK, paraview only GUI. If you feel like a real visualization wiz, use VTK or OpenGL from C++ and interface to Python with Cython. – Sturla Molden Mar 28 '14 at 16:14
  • Ok now I'm able to plot every single point in a graph but as I said the number of points is quite large (>40000) so It won't be very easy to manipulate. My question is: is it possible to do an interpolation between these points to see surface (possibly transparent just like in paraview) but in a python figure (so with matplotlib or mayavi) .. – user3473016 Mar 28 '14 at 16:27
  • Just like this figure: http://stackoverflow.com/questions/13631236/graph-a-colored-cube-in-matplotlib – user3473016 Mar 28 '14 at 16:34
  • Absolutely. But I don't remember on top of my head which VTK filter to use. – Sturla Molden Mar 28 '14 at 17:35
  • http://docs.enthought.com/mayavi/mayavi/ – Sturla Molden Mar 28 '14 at 17:36

4 Answers4

9

This is complicated data to view, so I think you'll need a tool designed to make viewing of 3D data easy, and MayaVi is an excellent option for this.

Here's an example,

enter image description here

And the most important aspect of this is that it's highly interactive, so using the mouse I can easily grab and move around the slice planes. and even tilt them to explore the data volumetric data (which is very useful, as in this case we can see it's mostly red on the inside, which we couldn't have guessed from just the surface):

enter image description here

Here's the code, which is just a slightly modified version of this:

from mayavi import mlab
import numpy as np

x, y, z = np.ogrid[-2:2:20j, -2:2:20j, -2:2:20j]
s = np.sin(x*y*z + x + y*z)/(x*y*z + x + y*z)

mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(s),
                            plane_orientation='x_axes',
                            slice_index=20,
                        )
mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(s),
                            plane_orientation='y_axes',
                            slice_index=20,
                        )
mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(s),
                            plane_orientation='z_axes',
                            slice_index=20,
                        )
mlab.outline()
mlab.show()
tom10
  • 67,082
  • 10
  • 127
  • 137
3

Check matplotlib, it is a nice and well-illustrated python plotting module, you should find what you need !

A first example

Specific 3D example

floppy12
  • 1,045
  • 6
  • 12
  • Yes of course I go trough it for hours .. Not able still to find a proper answer .. But thanks for this first hand – user3473016 Mar 28 '14 at 14:47
  • Sorry but I can't find a relevant example in their library .. When they talk about 3D plot they mean Z=f(X,Y). Actually I would like to draw a pseudo 4D plot: T=f(X,Y,Z) where X,Y,Z are located on a regular cartesian grid .. – user3473016 Mar 28 '14 at 15:05
  • [This](http://stackoverflow.com/a/14790650/1461850) seems to be what you want? – Lee Mar 28 '14 at 16:03
2

I don't understand so well what you want. But if you are intended to de a 4D plot you will need a fourth dimension (do you have an example of what you want?). I used the color as another dimension, in this example I plotted a Gaussian function over R^3 centered at (0,0,0) and the color of each point gives the value of the function.

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X, Y, Z = np.mgrid[-1:1:10j, -1:1:10j, -1:1:10j]

T = np.exp(-X**2 - Y**2 - Z**2)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
scat = ax.scatter(X, Y, Z, c=Z.flatten(), alpha=0.5)
fig.colorbar(scat, shrink=0.5, aspect=5)

enter image description here

For the interpolation part you can use the scipy.interpolate.

nicoguaro
  • 3,629
  • 1
  • 32
  • 57
  • That's what I wanted: the color expresses the fourth dimension. I just have now to interpolate between the points to get surface instead of points .. But will it be trivial ? – user3473016 Mar 28 '14 at 16:15
  • that's the kind of result I would like to get http://stackoverflow.com/questions/13631236/graph-a-colored-cube-in-matplotlib – user3473016 Mar 28 '14 at 16:33
  • Try [Mayavi](http://docs.enthought.com/mayavi/mayavi/) as tom10 suggested. Furthermore, the use of surface of constant value can be useful. In Mayavi you can use it with [Contour3d](http://scipy-lectures.github.io/packages/3d_plotting/index.html) – nicoguaro Mar 28 '14 at 19:42
1

Mayavi might be another option

Lee
  • 29,398
  • 28
  • 117
  • 170