title: Normalizing Colormap Used by Facecolors in Matplotlib
body: <p>First off, I'm trying to plot spherical harmonics in matplotlib as they are seen here in mayavi: <a href="http://docs.enthought.com/mayavi/mayavi/auto/example_spherical_harmonics.html" rel="nofollow noreferrer">http://docs.enthought.com/mayavi/mayavi/auto/example_spherical_harmonics.html</a></p>
  
  <p>Here is where I'm at:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  from matplotlib import cm, colors
  from mpl_toolkits.mplot3d import Axes3D
  import numpy as np
  from scipy import special
  
  # Create a sphere
  r = 3
  pi = np.pi
  cos = np.cos
  sin = np.sin
  phi, theta = np.mgrid[0:pi:50j, 0:2*pi:50j]
  
  
  x = r * sin(phi) * cos(theta)
  y = r * sin(phi) * sin(theta)
  z = r * cos(phi)
  
  colorfunction=special.sph_harm(3,4,theta,phi).real
  
  norm=colors.Normalize(vmin = np.min(colorfunction), vmax = np.max(colorfunction), clip = False)
  print colorfunction
  
  
  
  fig = plt.figure()
  ax = fig.add_subplot(111, projection='3d')
  ax.scatter(x, y, z)
  ax.plot_surface(
      x, y, z,  rstride=1, cstride=1, norm=norm, cmap=cm.jet, facecolors=cm.jet(colorfunction))
  plt.show()
  </code></pre>
  
  <p>The idea is to use <code>colorfunction</code> to colour the surface of the sphere according to the spherical harmonic. However, the output of this function is an array with negative numbers. What I need to do is 'normalize' this array so it behaves nicely with matplotlib's colormap. However, unlike the answer here, <a href="https://stackoverflow.com/questions/6539944/color-matplotlib-plot-surface-command-with-surface-gradient">Color matplotlib plot_surface command with surface gradient</a> , where the answer simply preforms a sloppy normalize by dividing by the largest element, I have negative elements so that just won't work. I'd ideally like to use <code>matplotlib.colors.Normalize</code> but it just isn't working on facecolors. </p>
  
  <p>I know that the norm is applying to the <code>cmap=cm.jet</code>, because if I remove <code>facecolors</code> argument entirely I get a new colormap that behaves according to my <code>norm</code> function. </p>
  
  <p>This is the crux of my issue, I cannot get my normalized colormap to apply to my facecolors. Any ideas?</p>
  
  <p><img src="https://i.stack.imgur.com/QkJKv.png" alt="">
  This is the figure the above code currently generates. As you can see the negative values are cut-off entirely and the information is lost because the colormaps range is much larger than the actual values (so everything just looks blue).</p>