写一段代码在python中画三维图
时间: 2023-04-01 08:01:39 浏览: 68
以下是Python中画三维图的代码示例:
```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [5, 6, 2, 3, 13, 4, 1, 2, 4, 8]
z = [2, 3, 3, 3, 5, 7, 9, 11, 9, 10]
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
```
这段代码可以画出一个简单的三维散点图。如果需要画其他类型的三维图形,可以根据需要修改代码。
相关问题
python中画三维等高线图
以下是使用Matplotlib库在Python中绘制三维等高线图的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 创建数据
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
# 绘制等高线图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.contour(X, Y, Z)
# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 显示图形
plt.show()
```
这段代码首先创建了一个二维网格,然后根据网格坐标计算了对应的Z值。接下来,使用`contour`函数绘制了等高线图。最后,通过设置坐标轴标签和调用`show`函数来显示图形。
请帮我写一段python绘制三维散点图的代码
当然可以,以下是一段绘制三维散点图的Python代码:
```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [5, 6, 2, 3, 13, 4, 1, 2, 4, 8]
z = [2, 3, 3, 3, 5, 7, 9, 11, 9, 10]
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
```
这段代码使用了Matplotlib库和mplot3d工具包,可以绘制出一个简单的三维散点图。