根据变量在Matplotlib中更改表面颜色

问题描述

我已经使用Matplotlib将一个简单的函数绘制为3D图。现在,我想创建一个颜色图以根据uv函数中得出的速度对其进行着色。为此,我想使用在c中看到的功能。我应该怎么做呢?预先谢谢你。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-2,2,20)
y = np.linspace(-2,20)
X,Y = np.meshgrid(x,y)
h = np.exp(-X**2 - Y**2)            # plotted function
u = -2*X*np.exp(-X**2 - Y**2)       # speed in x-direction
v = -2*Y*np.exp(-X**2 - Y**2)       # speed in y-direction
c = np.sqrt(u**2 + v**2)            # colormap derives from this

fig = plt.figure(figsize = (8,7),facecolor='white')
ax = fig.add_subplot(projection='3d')
ax.plot_surface(X,Y,h)

plt.show()

解决方法

尝试一下

from matplotlib import cm

fig = plt.figure(figsize = (8,7),facecolor='white')
ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(X,Y,h,rstride=1,cstride=1,facecolors = cm.jet(c/np.amax(c)),linewidth=0,antialiased=False)

plt.show()

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...