python画三维空间中的点线图

该文展示了如何利用Plotly库生成3D空间中的散点图和线条图,并用matplotlib进行相同操作。文中通过示例代码创建了两个不同的3D路径,分别用红色和蓝色表示,同时在每个库中都包含了散点和线条两种模式,以呈现数据的不同视觉效果。
摘要由CSDN通过智能技术生成

使用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()

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值