1

I'm looking to graph a 4D data set (X, Y, Z, intensity) using opacity to represent intensity. I also want the color to be dependent on the Z variable as well to better show depth.

Here is the relevant code so far, I am a novice when it comes to Python:

.
.
.
x_list #list of x values as floats
y_list #list of y values as floats
z_list #list of z values as floats
i_list #list of intensity values as floats

.
.
.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Axes3D.scatter(ax, x_list, y_list, z_list)
.
.
.

So how can I do this?

I'm thinking the color could be a linear relationship between z_list and a color map (hsv for example), and opacity could be linear as well, i_list/max(i_list) or something along those lines.

NGXII
  • 407
  • 2
  • 9
  • 18

1 Answers1

1

I would do something like the following:

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

# choose your colormap
cmap = plt.cm.jet

# get a Nx4 array of RGBA corresponding to zs
# cmap expects values between 0 and 1
z_list = np.array(z_list) # if z_list is type `list`
colors = cmap(z_list / z_list.max())

# set the alpha values according to i_list
# must satisfy 0 <= i <= 1
i_list = np.array(i_list)
colors[:,-1] = i_list / i_list.max()

# then plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_list, y_list, z_list, c=colors)
plt.show()

Here's an example with x_list = y_list = z_list = i_list. You can choose any of the colormaps here or make your own: enter image description here

wflynny
  • 18,065
  • 5
  • 46
  • 67
  • Thank you so much! Though when I try to run this, I get the error "Unknown property depthshade", though without depthshade=False in the ax.scatter statement it compiles. Also, how do you remove the black outlines of each data point as shown in your graph? – NGXII Jun 22 '15 at 20:10
  • Ok, I removed the depthshade kwarg from my answer (though it's in the [API](http://matplotlib.org/mpl_toolkits/mplot3d/api.html#mpl_toolkits.mplot3d.axes3d.Axes3D.scatter)). You can try setting `lw=0` to remove the edges of the scatter points. – wflynny Jun 22 '15 at 20:14
  • I was able to remove the edges using edgecolors='none' , though you are correct in using depthshade=False, though it's not accepting it for some reason and giving me that error. Any ideas? – NGXII Jun 22 '15 at 20:19
  • My guess depthshade is a keyword added to a recent version of MPL as it doesn't look like it's there in version 1.3.x. What version are you using? My plot above was made with 1.4.3. – wflynny Jun 22 '15 at 20:35
  • I'm not sure, I'm using PyCharm 4.5.2, though depthshade is still there, I see it happening in my sample set, and since opacity is used to represent an important piece of data, this isn't a good thing.. – NGXII Jun 22 '15 at 20:38
  • I would try this: `import inspect` then `print inspect.getargspec(ax.scatter)`. Do this below where `ax` is defined. This should print out the arguments and keyword arguments that scatter can take. If you don't see depthshade, you may need to upgrade your matplotlib distribution ([via pycharm](https://www.jetbrains.com/pycharm/help/installing-uninstalling-and-upgrading-packages.html)) – wflynny Jun 22 '15 at 20:50