使用ploty
import plotly.graph_objs as go
# 生成数据
x1 = [1, 2, 3, 4, 5, 6]
y1 = [2, 4, 1, 5, 2, 6]
z1 = [3, 1, 5, 2, 6, 4]
x2 = [6, 5, 4, 3, 2, 1]
y2 = [1, 3, 2, 4, 5, 6]
z2 = [4, 5, 2, 6, 1, 3]
# 创建散点图和线图的轨迹对象
scatter1 = go.Scatter3d(x=x1, y=y1, z=z1, mode='markers', marker=dict(size=5), name='Line 1')
line1 = go.Scatter3d(x=x1, y=y1, z=z1, mode='lines', line=dict(color='red', width=2), name='Line 1')
scatter2 = go.Scatter3d(x=x2, y=y2, z=z2, mode='markers', marker=dict(size=5), name='Line 2')
line2 = go.Scatter3d(x=x2, y=y2, z=z2, mode='lines', line=dict(color='blue', width=2), name='Line 2')
# 创建数据列表
data = [scatter1, line1, scatter2, line2]
# 创建布局对象
layout = go.Layout(title='Two Lines in 3D Space', scene=dict(xaxis=dict(title='X'),
yaxis=dict(title='Y'),
zaxis=dict(title='Z')))
# 创建图形对象
fig = go.Figure(data=data, layout=layout)
# 显示图形
fig.show()
使用matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 生成数据
x1 = [1, 2, 3, 4, 5, 6]
y1 = [2, 4, 1, 5, 2, 6]
z1 = [3, 1, 5, 2, 6, 4]
x2 = [6, 5, 4, 3, 2, 1]
y2 = [1, 3, 2, 4, 5, 6]
z2 = [4, 5, 2, 6, 1, 3]
# 创建 3D 图形对象
fig = plt.figure()
ax = Axes3D(fig)
# 绘制散点图和连接线
ax.plot(x1, y1, z1, 'r')
ax.plot(x2, y2, z2, 'b')
# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 显示图形
plt.show()