No color scaling when using plot_surface. Please help

I am trying to plot some data over a mesh using the plot_surface
method. However when I plot my data, everything is the same color
when I expected to get a nice rainbow of colors as in the example:
http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo.html

I have attached a simple script to show what I did as well as the
result. Essentially, I just copied the above demo, but put my own
data in. I think the problem arises because I have "holes" in my
data, or areas where the data is zero. These zeros throw the scaling
off so I tried to eliminate their effect, but this messed everything
up.

Essentially my question is: how can I get a nice color distribution
while at the same time avoid the extreme scaling issues associated
with some data being zero (while all the other data is ~16)?

Thanks,
Jeremy

tmp.py (5.21 KB)

tmp.pdf (17.4 KB)

Jeremy,

The pcolor function can take a vmin and a vmax parameter if you wish to control the colorscaling. In addition, you can use a special array structure called a “masked array” to have pcolor ignore “special” values. Assuming your data is ‘vals’:

vals_masked = numpy.ma.masked_array(vals, vals == 0.0)

Note that depending on your situation, doing an equality with with a floating point value probably isn’t very reliable, so be sure to test and modify to suit your needs. ‘vals_masked’ can then be passed to pcolor instead of vals.

I hope this helps,
Ben Root

···

On Wed, Jun 30, 2010 at 9:39 AM, Jeremy Conlin <jlconlin@…287…> wrote:

I am trying to plot some data over a mesh using the plot_surface

method. However when I plot my data, everything is the same color

when I expected to get a nice rainbow of colors as in the example:

http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo.html

I have attached a simple script to show what I did as well as the

result. Essentially, I just copied the above demo, but put my own

data in. I think the problem arises because I have “holes” in my

data, or areas where the data is zero. These zeros throw the scaling

off so I tried to eliminate their effect, but this messed everything

up.

Essentially my question is: how can I get a nice color distribution

while at the same time avoid the extreme scaling issues associated

with some data being zero (while all the other data is ~16)?

Thanks,

Jeremy


This SF.net email is sponsored by Sprint

What will you do first with EVO, the first 4G phone?

Visit sprint.com/firsthttp://p.sf.net/sfu/sprint-com-first


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

It seems that plot_surface method does have some issue with NaNs.
I think it is best if you manually adjust the color range. For example,

vmin = np.nanmin(mass)
vmax = np.nanmax(mass)

ax.plot_surface(X, Y, mass, rstride=1, cstride=1, cmap=cm.jet,
                      vmin=vmin, vmax=vmax)

IHTH,

-JJ

···

On Wed, Jun 30, 2010 at 10:39 AM, Jeremy Conlin <jlconlin@...287...> wrote:

Essentially my question is: how can I get a nice color distribution
while at the same time avoid the extreme scaling issues associated
with some data being zero (while all the other data is ~16)?

Yes, I think this is exactly what I need. Thanks!

Jeremy

···

On Sun, Jul 4, 2010 at 8:38 PM, Benjamin Root <ben.root@...1304...> wrote:

Jeremy,

The pcolor function can take a vmin and a vmax parameter if you wish to
control the colorscaling. In addition, you can use a special array
structure called a "masked array" to have pcolor ignore "special" values.
Assuming your data is 'vals':

vals_masked = numpy.ma.masked_array(vals, vals == 0.0)

Note that depending on your situation, doing an equality with with a
floating point value probably isn't very reliable, so be sure to test and
modify to suit your needs. 'vals_masked' can then be passed to pcolor
instead of vals.

To follow up with my response, I tried the above and it works nicely
with pyplot.pcolor. I would like to get a 3D version of this, like I
get using Axes3D.plot_surface. Is this just not implemented yet? I
am using 0.99.1.1. Has this been implemented in matplotlib 1.0?

Thanks,
Jeremy

···

On Thu, Jul 8, 2010 at 8:41 AM, Jeremy Conlin <jlconlin@...287...> wrote:

On Sun, Jul 4, 2010 at 8:38 PM, Benjamin Root <ben.root@...1304...> wrote:

Jeremy,

The pcolor function can take a vmin and a vmax parameter if you wish to
control the colorscaling. In addition, you can use a special array
structure called a "masked array" to have pcolor ignore "special" values.
Assuming your data is 'vals':

vals_masked = numpy.ma.masked_array(vals, vals == 0.0)

Note that depending on your situation, doing an equality with with a
floating point value probably isn't very reliable, so be sure to test and
modify to suit your needs. 'vals_masked' can then be passed to pcolor
instead of vals.

Yes, I think this is exactly what I need. Thanks!

Jeremy,

I believe that 0.99.1 is fairly old. I don’t know when Axes3D came along, but I am sure you can find it in 0.99.3. It is most definitely in 1.0, but you might not need to go that far if your distro does not provide it.

Ben Root

···

On Fri, Jul 9, 2010 at 4:50 PM, Jeremy Conlin <jlconlin@…287…> wrote:

On Thu, Jul 8, 2010 at 8:41 AM, Jeremy Conlin <jlconlin@…287…> wrote:

On Sun, Jul 4, 2010 at 8:38 PM, Benjamin Root <ben.root@…1304…> wrote:

Jeremy,

The pcolor function can take a vmin and a vmax parameter if you wish to

control the colorscaling. In addition, you can use a special array

structure called a “masked array” to have pcolor ignore “special” values.

Assuming your data is ‘vals’:

vals_masked = numpy.ma.masked_array(vals, vals == 0.0)

Note that depending on your situation, doing an equality with with a

floating point value probably isn’t very reliable, so be sure to test and

modify to suit your needs. ‘vals_masked’ can then be passed to pcolor

instead of vals.

Yes, I think this is exactly what I need. Thanks!

To follow up with my response, I tried the above and it works nicely

with pyplot.pcolor. I would like to get a 3D version of this, like I

get using Axes3D.plot_surface. Is this just not implemented yet? I

am using 0.99.1.1. Has this been implemented in matplotlib 1.0?

Thanks,

Jeremy

Wince my first post, I have upgraded to 1.0 which definitely has
Axes3D. The trouble is that the plot_surface function does not deal
with masked arrays like color does. That is what I need and I haven't
found a way around it.

Jeremy

···

On Friday, July 9, 2010, Benjamin Root <ben.root@...1304...> wrote:

Jeremy,

I believe that 0.99.1 is fairly old. I don't know when Axes3D came along, but I am sure you can find it in 0.99.3. It is most definitely in 1.0, but you might not need to go that far if your distro does not provide it.

Ah, I misread your original post and thought you were talking about pcolor. I will take a look at plot_surface and see if there is a possible reason for your issue. I have an idea of what is happening, but I have to see the code when I get back to my office tomorrow.

Ben Root

···

On Sun, Jul 11, 2010 at 8:43 PM, Jeremy Conlin <jlconlin@…287…> wrote:

On Friday, July 9, 2010, Benjamin Root <ben.root@…1304…> wrote:

Jeremy,

I believe that 0.99.1 is fairly old. I don’t know when Axes3D came along, but I am sure you can find it in 0.99.3. It is most definitely in 1.0, but you might not need to go that far if your distro does not provide it.

Wince my first post, I have upgraded to 1.0 which definitely has

Axes3D. The trouble is that the plot_surface function does not deal

with masked arrays like color does. That is what I need and I haven’t

found a way around it.

Jeremy