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

基于Pandas DataSeries构建指定轴映射的3D Plot技术问询

没问题,我来帮你实现这个3D可视化需求!下面是基于Pandas数据构建3D图的完整方案:

基于Pandas DataFrame构建3D散点图实现方案

1. 导入所需工具库

首先咱们得把处理数据和绘图的库准备好:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

2. 整理数据集

根据你提供的样本数据,先把它转换成Pandas DataFrame(最后一行我补全了缺失的部分,你可以根据实际数据调整):

# 构建数据集
data = {
    'ex_one': [1, 10, 100, 100, 1000, 100001, 100001, 100001, 100001, 100001],
    'fake_date': ['2001-07-30', '2004-02-07', '2000-09-19', '2003-03-11', '2000-01-04',
                  '2000-01-16', '2000-02-01', '2000-02-08', '2000-10-18', '2000-12-20'],
    'ex_two': [695, 1405, 4832, 626, 3993, 213974, 136902, 100005, 213963, 150000],
    'weight': [1, 1, 1, 1, 1, 2, 1, 1, 1, 1]
}
df = pd.DataFrame(data)

3. 绘制3D关联图

接下来咱们创建3D散点图,用X轴对应ex_one,Y轴对应ex_two,Z轴对应weight,同时通过点的大小和颜色强化weight的视觉差异:

# 创建画布和3D坐标轴
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')

# 提取对应轴的数据
x = df['ex_one']
y = df['ex_two']
z = df['weight']

# 绘制散点图:用点大小体现weight差异,颜色映射辅助区分
scatter = ax.scatter(x, y, z, s=z*150, c=z, cmap='plasma', alpha=0.7)

# 设置坐标轴标签
ax.set_xlabel('ex_one', fontweight='bold')
ax.set_ylabel('ex_two', fontweight='bold')
ax.set_zlabel('weight', fontweight='bold')

# 添加颜色条,解释weight对应的颜色含义
plt.colorbar(scatter, label='Weight Value')

# 设置图表标题
ax.set_title('3D Plot: ex_one & ex_two Correlation by Weight', fontweight='bold', pad=20)

# 展示图形
plt.show()

4. 优化小技巧

  • 你的数据里ex_one数值跨度极大(从1到100001),可以给X轴做对数变换让图形更清晰:
    ax.set_xscale('log')
    
  • 点的大小参数s=z*150是缩放因子,你可以根据实际显示效果调整,确保不同weight的点有明显区分;
  • 如果想展示关联趋势,也可以尝试3D折线图,但散点图更适合呈现离散的关联点分布。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:51:28