3

I was wondering if there's a way to plot a data cube in Python. I mean I have three coordinate for every point

x=part.points[:,0]
y=part.points[:,1]
z=part.points[:,2]

And for every point I have a scalar field t(x,y,z)

I would like to plot a 3D data cube showing the position of the point and for every point a color which is proportional to the scalar field t in that point.

I tried with histogramdd but it didn't work.

smci
  • 32,567
  • 20
  • 113
  • 146
Brian
  • 13,996
  • 19
  • 70
  • 94

2 Answers2

10

You can use matplotlib. Here you have a working example (that moves!):

import random
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D

mypoints = []
for _ in range(100):
    mypoints.append([random.random(),    #x
                    random.random(),     #y
                    random.random(),     #z
                    random.randint(10,100)]) #scalar

data = zip(*mypoints)           # use list(zip(*mypoints)) with py3k  

fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(data[0], data[1], data[2], c=data[3])
pyplot.show()

enter image description here

You probably have to customize the relation of your scalar values with the corresponding colors.
Matplotlib has a very nice look but it can be slow drawing and moving these 3D drawings when you have many points. In these cases I used to use Gnuplot controlled by gnuplot.py. Gnuplot can also be used directly as a subprocess as shown here and here.

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • thanks! Can I ask you also how to add the zlabel, apart from the obvious x and y labels? I tried with pyploy.zlabel("my_label") but it didn't work. Moreover, if the points are too much this code get stuck. How can I apply them a gaussian filter to smooth them and still have a nice 3D plot? – Brian Jul 31 '11 at 09:04
  • Another thing. How can I add a title to the plot and a colorbar show the legend for the colors? – Brian Jul 31 '11 at 09:20
  • ax.set_zlabel('Z'). ax.set_title('Mi Title'). You can experiment for the others or maybe to ask SO. – joaquin Jul 31 '11 at 10:20
0

Another option is Dots plot, produced by MathGL. It is GPL plotting library. Add it don't need many memory if you save in bitmap format (PNG, JPEG, GIF and so on).

abalakin
  • 825
  • 7
  • 6