scatter3D

Hi All,

I have been trying to make a 3D scatter plot using mplot3d and I would
like the markers to have their colour according to the Z value.

From what I understood in the tutorial and API I have to use the cmap

and norm kwargs, but all my attempts failed.

I am trying to do it like this:
ax.scatter(x,y,z,s=10,marker='o',c=???,cmap=???,norm=???)

However I am not sure what to pass to c, cmap and norm.

Any help ?
Thanks.

Cheers,
Pedro

It has been a while since I played around with this, and I am working completely off my memory right now, but here goes…

If I remember correctly, the ‘c’ values can be an array that is parallel to the x, y, z arrays and specifies the color in one of two ways. First, the array could have a list of color specs (e.g., ‘k’, ‘r’, ‘b’). Second, the array could contain values that would be passed to the colormap to retrieve the colorspec according to where the value lies on the scale of 0 to 1.

If you want to use just simple colors, go ahead and just make a list of characters like so:

[‘k’, ‘r’, ‘g’, ‘g’, ‘b’, ‘k’]

based on whatever the values are in z (I recommend using numpy’s where() function for this.

I am not 100% sure if you need the following or if mpl will just autoscale for you. So you might want to first just try out using the ‘c’ keyword.

If you want to use the colormap, then the values passed into c either has to be the normalized values of z

c = (z - z.min()) / (z.max()-z.min())

or you can use one of the Normalize classes in matplotlib.color (assuming you have imported matplotlib.pyplot as plt):

norm = plt.Normalize(z.min(), z.max())

or whatever minimum and/or maximum values you wish.

I believe you can use the default colormap by not specifying anything at all for cmap, but I could be wrong here. You can also specify any colormap by their name like ‘spring’, ‘jet’, ‘bone’ and so on.

I hope that helps.
Ben Root

···

On Wed, Oct 13, 2010 at 5:46 PM, Pedro M. Ferreira <pmffferreira@…287…> wrote:

Hi All,

I have been trying to make a 3D scatter plot using mplot3d and I would

like the markers to have their colour according to the Z value.

From what I understood in the tutorial and API I have to use the cmap

and norm kwargs, but all my attempts failed.

I am trying to do it like this:

ax.scatter(x,y,z,s=10,marker=‘o’,c=???,cmap=???,norm=???)

However I am not sure what to pass to c, cmap and norm.

Any help ?

Thanks.

Cheers,

Pedro

Well, it did help at least to understand a bit more, although I still
fail to do it.

The code in the file axes3d.py says that keyword arguments passed to
scatter3D are passed on to matplotlib.scatter, so I would expect the
following two figures to work similarly in terms of color:

import matplotlib as mpl
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

f1=mpl.figure()
mpl.scatter(X,Z,c=np.abs(Z/3.0)) # My Z is in [-3.0, 3.0]

f2=mpl.figure()
ax=Axes3D(f2)
ax.scatter3D(X,Y,Z,c=abs(Z/3.0))

mpl.show()

It happens that f1 shows what I expect, a scatter in 2D with the
colors of markers mapped to a colormap (I believe its jet).
For f2, the markers appear as white filled circles.

I am using python 2.6.6 in Debian squeeze (amd64) with
python-matplotlib version 0.99.3-1

Thanks for any help or comment.

···

On Thu, Oct 14, 2010 at 1:59 AM, Benjamin Root <ben.root@...1304...> wrote:

On Wed, Oct 13, 2010 at 5:46 PM, Pedro M. Ferreira <pmffferreira@...1896....> > wrote:

Hi All,

I have been trying to make a 3D scatter plot using mplot3d and I would
like the markers to have their colour according to the Z value.
>From what I understood in the tutorial and API I have to use the cmap
and norm kwargs, but all my attempts failed.

I am trying to do it like this:
ax.scatter(x,y,z,s=10,marker='o',c=???,cmap=???,norm=???)

However I am not sure what to pass to c, cmap and norm.

Any help ?
Thanks.

Cheers,
Pedro

It has been a while since I played around with this, and I am working
completely off my memory right now, but here goes...

If I remember correctly, the 'c' values can be an array that is parallel to
the x, y, z arrays and specifies the color in one of two ways. First, the
array could have a list of color specs (e.g., 'k', 'r', 'b'). Second, the
array could contain values that would be passed to the colormap to retrieve
the colorspec according to where the value lies on the scale of 0 to 1.

If you want to use just simple colors, go ahead and just make a list of
characters like so:

['k', 'r', 'g', 'g', 'b', 'k']

based on whatever the values are in z (I recommend using numpy's where()
function for this.

I am not 100% sure if you need the following or if mpl will just autoscale
for you. So you might want to first just try out using the 'c' keyword.

If you want to use the colormap, then the values passed into c either has to
be the normalized values of z

c = (z - z.min()) / (z.max()-z.min())

or you can use one of the Normalize classes in matplotlib.color (assuming
you have imported matplotlib.pyplot as plt):

norm = plt.Normalize(z.min(), z.max())

or whatever minimum and/or maximum values you wish.

I believe you can use the default colormap by not specifying anything at all
for cmap, but I could be wrong here. You can also specify any colormap by
their name like 'spring', 'jet', 'bone' and so on.

I hope that helps.
Ben Root

Ah, that might explain the problem! I don’t have the time to verify this now, but I seem to remember doing a bug fix where some of the parameters being passed into scatter3d() was being ‘swallowed’ and not passed down to scatter(). You might need to upgrade to the maintenance branch of mpl v1.0.0 in svn. Note that the problem was probably fixed after the release of v1.0.0 if I remember it correctly, so you will need the latest revised release from source control.

If you need help with that, just let us know.

Ben Root

···

On Thu, Oct 14, 2010 at 4:54 AM, Pedro M. Ferreira <pmffferreira@…287…> wrote:

Well, it did help at least to understand a bit more, although I still

fail to do it.

The code in the file axes3d.py says that keyword arguments passed to

scatter3D are passed on to matplotlib.scatter, so I would expect the

following two figures to work similarly in terms of color:

import matplotlib as mpl

import numpy as np

from mpl_toolkits.mplot3d import Axes3D

f1=mpl.figure()

mpl.scatter(X,Z,c=np.abs(Z/3.0)) # My Z is in [-3.0, 3.0]

f2=mpl.figure()

ax=Axes3D(f2)

ax.scatter3D(X,Y,Z,c=abs(Z/3.0))

mpl.show()

It happens that f1 shows what I expect, a scatter in 2D with the

colors of markers mapped to a colormap (I believe its jet).

For f2, the markers appear as white filled circles.

I am using python 2.6.6 in Debian squeeze (amd64) with

python-matplotlib version 0.99.3-1