You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何绘制3D图形:已计算系数前提下的3D绘图实现方法

Python 3D图形绘制修正方案

原代码存在两个可直接运行的问题:

  • 没有正确导入linalg模块,直接运行会报名称错误
  • 仅初始化了3D绘图坐标轴,没有传入任何绘图数据,运行后只会显示空白画布

以下是修复后的完整可运行代码:

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

a = np.array([[6, 2, 3, 1], [-3, -2, 4, 1], [5, -6, 2, 1], [-1, -1, 6, 0]])
b = np.array([28, 31, 53, -34])
# 修复linalg调用问题
θ = np.linalg.solve(a, b)
print("求解得到的参数:", θ)

fig = plt.figure()
ax = plt.axes(projection='3d')

# 生成x、y网格数据,范围、采样密度可根据实际需求调整
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(x, y)

# 这里默认参数对应模型为 z = θ0*x + θ1*y + θ2*x*y + θ3,可替换为你的实际计算模型
Z = θ[0] * X + θ[1] * Y + θ[2] * X * Y + θ[3]

# 绘制3D曲面,可替换为plot_wireframe(网格线)、scatter(散点)等其他3D绘图接口
ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)

# 按需添加坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

内容的提问来源于stack exchange,提问作者W Z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 03:18:04