简体   繁体   English

如何使用 function plot_surface 中的 plot 3D 图?

[英]How to plot 3D diagram using function plot_surface in matplotlib?

My code is shown as below:我的代码如下所示:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

x_points = np.linspace(1, 3, num=3)
y_points = np.linspace(1, 6, num=6)
z_points = np.linspace(1, 10, num=10)

x_points, y_points, z_points = np.meshgrid(x_points, y_points, z_points)
x_points = x_points.flatten().reshape(60, 3)
y_points = y_points.flatten().reshape(60, 3)
z_points = z_points.flatten().reshape(60, 3)

# To simplify the problem and illustrate my point, suppose that there exists some kind of function
# relationship among x_points, y_points, z_points and u values.
u = f(x_points, y_points, z_points)

# bounds for normalization: minSpeed & maxSpeed
u = (u - minSpeed) / (maxSpeed - minSpeed)
fig = plt.figure()
ax = fig.gca(projection='3d')
im = ax.plot_surface(x_points, y_points, z_points, facecolors=cm.coolwarm(u), linewidth=0, antialiased=False, shade=False)

I want to plot 3D diagram with colors indicated by u values.我想 plot 3D 图用 u 值表示 colors。 I searched on the Internet about function plot_surface, and wrote the above code.我在网上搜索了关于function plot_surface,写了上面的代码。 But it didn't give a plot as I expected.但它没有像我预期的那样给出 plot 。 How to revise the code to achieve my purpose?如何修改代码以达到我的目的?

plot_surface allows to display the variations of a function Z of 2 variables (for instance Z=np.sqrt(X**2 + Y**2) ), but what you are considering here is a function U of 3 variables X, Y and Z. plot_surface允许显示 function Z 的 2 个变量的变化(例如Z=np.sqrt(X**2 + Y**2) ),但您在这里考虑的是 function U 的 3 个变量 X, Y和Z。

I suggest that you get familiar with plot_surface through the source code example provided here .建议您通过此处提供的源代码示例熟悉plot_surface

If you are indeed trying to display a "4D plot", check out this answer .如果您确实在尝试显示“4D 图”,请查看此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM