【发布时间】:2026-02-10 18:05:02
【问题描述】:
我有 2 个图,一个 3D 高斯和一个 2D 高斯。
我想更改 3D 高斯底部的颜色(对于 X 和 Y = 0),使其看起来像 2D 高斯(边缘为蓝色,靠近底部时颜色变为红色表面)。
如果我修改“Z”变量,3D 高斯的形状也会发生变化(我希望保持不变)。
您知道如何修改 3D 高斯中的“颜色”矩阵并插入 2D 中的颜色信息吗?
谢谢
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()
【问题讨论】:
标签: python arrays numpy matplotlib colors