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

Python 如何将列表内多个dataframe绘制到同一张3D散点图中

多DataFrame合并绘制3D散点图的代码修正方案

问题原因

你的代码存在两个核心错误导致只绘制了单个DataFrame的数据:

  • 遍历crystal_list的循环中,ind/tal/ial三个变量每次都会被新的DataFrame数据覆盖,循环结束后仅保留了最后一个DataFrame的对应数据
  • 你错误将整个DataFrame赋值给了X轴变量ind,但按需求X轴应该是当前DataFrame在列表中的固定索引值,而非DataFrame本身

修正后代码

import os
import pandas as pd
from os import listdir
from os.path import isfile, join
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 读取所有数据文件存入列表
file_list = [f for f in listdir("Rohdaten_zeitreihen/") if isfile(join("Rohdaten_zeitreihen/", f))]
crystal_list = []
for file_name in file_list:
    crystal = pd.read_csv(os.path.join("Rohdaten_zeitreihen/",file_name), sep="\s", skiprows=[1], engine="python")
    crystal_list.append(crystal)

# 初始化3D画布
plt.rcParams["figure.figsize"] = 15.8, 12.6
ax = plt.axes(projection='3d')

# 逐个绘制每个DataFrame的所有数据点
for df_index, df in enumerate(crystal_list):
    # X轴为当前DataFrame在列表中的索引,生成与当前df行数相同的X值数组
    x_axis = [df_index] * len(df)
    y_axis = df['time']
    z_axis = df['IR']
    ax.scatter(x_axis, y_axis, z_axis, c=z_axis, cmap='plasma', linewidth=1)

# 设置坐标轴标签
ax.set_xlabel("列表索引")
ax.set_ylabel("time")
ax.set_zlabel("IR")
plt.show()

可选优化

如果需要按不同DataFrame区分颜色,只需将scatter方法的c参数改为df_index即可,同个DataFrame的所有点会显示为相同颜色,不同DataFrame颜色不同。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 09:09:07