python画3维图
时间: 2023-09-10 20:12:26 浏览: 120
你可以使用一些库来绘制3D图形,常用的有matplotlib和plotly。下面是使用这两个库绘制3D图的简单示例:
使用matplotlib库:
```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 创建一个3D图形对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 生成数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
z = [3, 6, 9, 12, 15]
# 绘制3D散点图
ax.scatter(x, y, z)
# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 显示图形
plt.show()
```
使用plotly库:
```python
import plotly.graph_objects as go
# 生成数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
z = [3, 6, 9, 12, 15]
# 创建3D散点图对象
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers')])
# 设置坐标轴标签
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
# 显示图形
fig.show()
```
这些示例可以帮助你开始绘制简单的3D图形,你可以根据自己的需求进行进一步的定制和调整。
阅读全文
相关推荐















