Modify color surfaces in Python

Viewed 273

I have 2 graphs, a 3D gaussian and a 2D gaussian.

I would like to change the color at the base of the 3D gaussian (for X and Y = 0), so that it looks like the 2D one (blue at the edges, and color becomes red when getting closer to the base of the surface).

If I modify the "Z" variable, also the shape of the 3D gaussian changes (and I would like to keep it intact).

Do you know how I can modify the "color" matrix in the 3D gaussian and insert the color information from the 2D one?

Thanks

import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D # <--- This is important for 3d plotting
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
from math import *```

# GRAPH 1

# Parameters and make 3d Gaussian
A = 1
x0 = 0
y0 = 0
sigma_X = 1.5
sigma_Y = 1.5
xg = np.linspace(-5,5,num=100)
yg = np.linspace(-5,5,num=100)
theta= np.pi

X, Y = np.meshgrid(xg,yg)

a = np.cos(theta)**2/(2*sigma_X**2) + np.sin(theta)**2/(2*sigma_Y**2);
b = -np.sin(2*theta)/(4*sigma_X**2) + np.sin(2*theta)/(4*sigma_Y**2);
c = np.sin(theta)**2/(2*sigma_X**2) + np.cos(theta)**2/(2*sigma_Y**2);

aXXdet = np.array([a*(Xi-x0)**2 for Xi in X],float)
bbXYdet = np.array([2*b*(Xi-x0)*(Y[ii]-y0) for ii,Xi in enumerate(X)],float)
cYYdet = np.array([c*(Yi-y0)**2 for Yi in Y],float)
Z = np.array([A*np.exp( - (ai + bbXYdet[i] + cYYdet[i])) for i,ai in enumerate(aXXdet)],float);

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

# Select Color and Plot Surface graph
colors = plt.cm.plasma_r( (Z-Z.min())/float((Z-Z.min()).max()))
fff = ax.plot_surface(X,Y,Z ,facecolors=colors, linewidth=1, shade=False,rstride=1, cstride=1, alpha=None, antialiased=True )
ax.view_init(27,45)
plt.show()


# GRAPH 2

# Make Gaussian as base for x and y = 0
from scipy import signal
def gkern(kernlen=100, std=30):
    """Returns a 2D Gaussian kernel array."""
    gkern1d = signal.gaussian(kernlen, std=std).reshape(kernlen, 1)
    gkern2d = np.outer(gkern1d, gkern1d)
    return gkern2d
plt.imshow(gkern(100), interpolation='none', cmap = "plasma")
plt.show()

Graph1 Graph2

The result should look something like this, but without the yellow contrast at the base of the surface, it should all be continuous. Graph3

1 Answers

Something like this?

colors = plt.cm.plasma_r(abs(0.1-Z) + abs(X/15) + abs(Y/15))

Smooth 3D cone

Related