I have created a 3d plot of a sphere in python using Mathplotlib using the code below
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0,2*np.pi, 32)
v = np.linspace(0, np.pi, 16)
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=4, cstride=4, color='b')
plt.show()
Picture of plot:-
I would like to color each individual box a different color. I have tried to use a color map, but I was only able to change the color based on the z value with this.
Any suggestions would be greatly appreciated. I am open to using other tools or languages to accomplish this task, but I need the boxes to be the same size.
@Armatita's solution is very elegant, especially if you have a mapping function. I upvoted it :). Another solution for arbitrary shapes can be done with
Poly3DCollection
. Now you could import actual data and the surfaces could be arbitray in space and wouldn't have to connect.For comparison, however I used the same sphere. I edited the answer to assign the color as a function of the two angles based on your comments below.
Besides the link given in comments it's possible to directly map a variable to the colormap. Check your example adapted with a solution:
, this particularly bad example results in this: