简体   繁体   中英

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. I searched on the Internet about function plot_surface, and wrote the above code. But it didn't give a plot as I expected. 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.

I suggest that you get familiar with plot_surface through the source code example provided here .

If you are indeed trying to display a "4D plot", check out this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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