【Python】三维绘图

一、绘制三维图像

本文画的三维图x和y的长度须一致。

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

length = 100
x = []
x = [i for i in range(-1 * length,length)]
Y = X = np.array(x)
X, Y = np.meshgrid(X, Y)
z = X*X + Y*Y
# z = 5*X*X + 8*Y*Y*Y
Z = np.array(z).reshape(length*2,length*2)

# 绘制表面
fig = plt.figure()

ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(5))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# 添加将值映射到颜色的颜色栏
fig.colorbar(surf, shrink=0.5, aspect=5)

运行结果图:

猜你喜欢

转载自blog.csdn.net/weixin_46163097/article/details/124797142
今日推荐