colormap: values to colors

Hello

I am trying to calculate the facecolors for the Axes3D.plot_surface function, to have something similar to matlab surf 4th argument!!
Something like plot_surface(x,y,z,facecolors=calc_colors(v))

So, I need to obtain the colors corresponding to the values of v! I created the function calc_colors:

import pylab
import numpy as np
from matplotlib.colors import rgb2hex

def calc_colors(var):
  colors = np.empty(var.shape, dtype='S7')
  mm=pylab.cm.ScalarMappable()
  mm.set_clim((var.min(),var.max()))
  mm.set_cmap(pylab.cm.jet)
  colors.flat= [rgb2hex(mm.to_rgba(k)[:-1]) for k in var.flat]
  return colors

Well I believe this is not the smartest way to calculate the colors !! At least it is quite slow !
Can someone give me a better solution for this problem?

than you
mma

I might be wrong, but can’t mm.to_rgba() take var.flat as an input, thereby giving you an array of colors as output? Also, I don’t think it is necessary to call rgb2hex() because matplotlib will just convert that hex back into rgba. I believe you should be able to just return the result of mm.to_rgba(var.flat).

I hope that helps,
Ben Root

···

On Fri, Sep 17, 2010 at 8:59 AM, Martinho MA <mma@…1770…> wrote:

Hello

I am trying to calculate the facecolors for the Axes3D.plot_surface

function, to have something similar to matlab surf 4th argument!!

Something like plot_surface(x,y,z,facecolors=calc_colors(v))

So, I need to obtain the colors corresponding to the values of v! I

created the function calc_colors:

import pylab

import numpy as np

from matplotlib.colors import rgb2hex

def calc_colors(var):

colors = np.empty(var.shape, dtype=‘S7’)

mm=pylab.cm.ScalarMappable()

mm.set_clim((var.min(),var.max()))

mm.set_cmap(pylab.cm.jet)

colors.flat= [rgb2hex(mm.to_rgba(k)[:-1]) for k in var.flat]

return colors

Well I believe this is not the smartest way to calculate the colors !!

At least it is quite slow !

Can someone give me a better solution for this problem?

than you

mma

mm.to_rgba(var.flat) doesn't work as plot_surface facecolors, but mm.to_rgba(var) does!

Thanks for the hint

mma

Benjamin Root wrote:

···

On Fri, Sep 17, 2010 at 8:59 AM, Martinho MA <mma@...1770... > <mailto:mma@…1770…>> wrote:

    Hello

    I am trying to calculate the facecolors for the Axes3D.plot_surface
    function, to have something similar to matlab surf 4th argument!!
    Something like plot_surface(x,y,z,facecolors=calc_colors(v))

    So, I need to obtain the colors corresponding to the values of v! I
    created the function calc_colors:

    import pylab
    import numpy as np
    from matplotlib.colors import rgb2hex

    def calc_colors(var):
     colors = np.empty(var.shape, dtype='S7')
     mm=pylab.cm.ScalarMappable()
     mm.set_clim((var.min(),var.max()))
     mm.set_cmap(pylab.cm.jet)
     colors.flat= [rgb2hex(mm.to_rgba(k)[:-1]) for k in var.flat]
     return colors

    Well I believe this is not the smartest way to calculate the colors !!
    At least it is quite slow !
    Can someone give me a better solution for this problem?

    than you
    mma

I might be wrong, but can't mm.to_rgba() take var.flat as an input, thereby giving you an array of colors as output? Also, I don't think it is necessary to call rgb2hex() because matplotlib will just convert that hex back into rgba. I believe you should be able to just return the result of mm.to_rgba(var.flat).

I hope that helps,
Ben Root