how to create a facecolors map?

Hello,

I would like to create a surface with a color provided by an independent
variable. The shape is defined by a matrix with the z-values. The
color is a matrix of identical shape with floats. Can this be done?
My experiment code is incomplete: I could not write anything useful. Can
onyone help me making it work?

Thank you

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

no=100
cdict = {'red': ((0.0, 0.0, 0.0),
(15.0, 1.0, 1.0)),

'green': ((0.0, 0.0, 0.0),
(15.0, 0.0, 0.0)),

'blue': ((0.0, 1.0, 1.0),
(15.0, 0.0, 0.0))}

fig = plt.figure()
ax = fig.gca(projection = '3d')
x = np.linspace(-5,5,no)
y = np.linspace(-5,5,no)
x,y = np.meshgrid(x,y)
R = np.sqrt(x**2 + y**2)
z = np.sin(R)
D = np.sqrt((x-5)**2 + (y-5)**2)
#and now I need to map distance D
#making (-5,-5) blue and (5,5) red
c=

surf = ax.plot_surface(x, y, z, facecolors=c, rstride = 1, cstride = 1, cmap = jet, linewidth = 0)
ax.set_zlim3d(-2, 2)
plt.show()

Got something working:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

no=100

···

On Wed, Aug 28, 2013 at 06:39:26PM +0200, vwf wrote:

Hello,

I would like to create a surface with a color provided by an independent
variable.

#========================================
# color trick to draw with varying alpha
# http://matplotlib.1069221.n5.nabble.com/scatter-plot-individual-alpha-values-td21106.html
#========================================

from matplotlib.colors import LinearSegmentedColormap

class LinearColormap(LinearSegmentedColormap):

def __init__(self, name, segmented_data, index=None, **kwargs):
if index is None:
# If index not given, RGB colors are evenly-spaced in
# colormap.
index = np.linspace(0, 1, len(segmented_data['red']))
for key, value in segmented_data.iteritems():
# Combine color index with color values.
segmented_data[key] = zip(index, value)
segmented_data = dict((key, [(x, y, y) for x, y in value])
for key, value in
segmented_data.iteritems())
LinearSegmentedColormap.__init__(self, name, segmented_data, **kwargs)

color_spec = {'blue': [0.0, 0.0],
'green': [1.0, 0.0],
'red': [0.0, 1.0],
'alpha': [0.1, 1.0]}
alpha_color = LinearColormap('alpha_color', color_spec)

#==========================================

fig = plt.figure()
ax = fig.gca(projection = '3d')
x = np.linspace(-5,5,no)
y = np.linspace(-5,5,no)
x,y = np.meshgrid(x,y)
R = np.sqrt(x**2 + y**2)
z = np.sin(R)
D = np.sqrt((x-5)**2 + (y-5)**2)
c=np.zeros(shape=(no,no),dtype=object)

for i in range(no):
for j in range(no):
c[i,j]=alpha_color(D[i,i]/15)

surf = ax.plot_surface(x, y, z, facecolors=c, rstride = 1, cstride = 1, linewidth = 0)
ax.set_zlim3d(-2, 2)
plt.show()