Matplotlib colored sphere
<p>I have a data set which maps a tuple of phi and theta to
a value which represents the strength of the signal.
I want to plot these on a sphere. I simply followed 
a demo from matplotlib and adjusted the code to my
use case. </p>

<pre><code>from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

u = phi
v = theta
vals =vals/vals.max()
Map = cm.coolwarm
facecolors = Map(vals[:])

x = 10 * np.outer(np.cos(u), np.sin(v))

y = 10 * np.outer(np.sin(u), np.sin(v))

z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False, facecolors=facecolors)

plt.show()
</code></pre>

<p>This generates an error message IndexError: index 4 is out of bounds for axis 0 with size 4. I also looked into the source code, which seems
to indicate to me that facecolors isn't formatted correctly, but I'm
struggling to figure out, what formatting is needed exactly.</p>

<p>Any help or other ways to achieve this goal would be greatly 
appreciated.</p>

<p>Greetings</p>

<p>If your question is: "How to get rid of this IndexError?", I modified your code and now it works. <code>plot_surface</code> takes X,Y,Z and facecolors as 2D arrays of corresponding <strong>values on a 2D grid</strong>. Facecolors in your case weren't and this was the source of your error.  </p>

<pre><code>from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm, colors
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

u, v = np.mgrid[0:np.pi:50j, 0:2*np.pi:50j]

strength = u
norm=colors.Normalize(vmin = np.min(strength),
                      vmax = np.max(strength), clip = False)

x = 10 * np.sin(u) * np.cos(v)
y = 10 * np.sin(u) * np.sin(v)
z = 10 * np.cos(u)

ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False,
                       facecolors=cm.coolwarm(norm(strength)))

plt.show()
</code></pre>

<p>Result is this <a href="http://i.stack.imgur.com/pIpSd.png" rel="nofollow">image</a> of a sphere.</p>

<p>However, if your data is <strong>not on a 2D grid</strong> you are in trouble. Additionally if your grid is not regular the sphere you plot will look irregular as well. So if your question is: "<em>How to plot a heatmap on a sphere?</em>", there is already such a question and solution <a href="http://stackoverflow.com/questions/22128909/plotting-the-temperature-distribution-on-a-sphere-with-python">here</a> using <code>Basemap</code> package produces this result:
<a href="http://i.stack.imgur.com/8QDxC.png" rel="nofollow"><img src="http://i.stack.imgur.com/8QDxC.png" alt="enter image description here"></a></p>

python matplotlib