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

如何绘制X轴为时间、Y轴为深度的单父图展示振幅数据?

问题描述

现有随时间在不同深度记录的振幅数据集:

data = {
    'Time': [0, 2.5, 5, 7.5, 10],
    'Depth_63.5': [161, 143, 134, 147, 163],
    'Depth_64.5': [183, 190, 375, 255, 241],
    'Depth_65.5': [711, 727, 914, 756, 747],
}

当前用Matplotlib和Pandas绘制的是时间vs振幅的折线图,但期望生成X轴为时间、Y轴为深度的单图,推测可能需要多Y轴关联方案,当前代码如下:

import matplotlib.pyplot as plt
import pandas as pd

# 示例数据
data = {
    'Time': [0, 2.5, 5, 7.5, 10],
    'Depth_63.5': [161, 143, 134, 147, 163],
    'Depth_64.5': [183, 190, 375, 255, 241],
    'Depth_65.5': [711, 727, 914, 756, 747],
}

# 转换为DataFrame
df = pd.DataFrame(data)

# 提取深度值
depth_levels = [col.split('_')[1] for col in df.columns if 'Depth_' in col]

# 创建画布和轴
fig, ax = plt.subplots(figsize=(10, 6))

# 循环绘制每个深度的时间vs振幅折线
for depth_level in depth_levels:
    ax.plot(df['Time'], df[f'Depth_{depth_level}'], label=f'Depth {depth_level}')

# 自定义图表样式
ax.set_title('Depth vs Time vs Amplitude')
ax.set_xlabel('Time (us)')
ax.set_ylabel('Depth Levels')
ax.legend()
ax.grid(True)

# 显示图表
plt.show()
解决方案

要实现X轴为时间、Y轴为深度的可视化,不需要复杂的多Y轴方案,只需把深度作为Y轴的固定刻度值,将每个时间点对应的振幅用标注、线条或颜色体现即可。以下是两种直观的实现方式:

方式1:带振幅标注的横向轨迹图

将每个深度值映射为Y轴的固定位置,用线条连接同一深度下不同时间点的坐标,同时标注对应振幅值:

import matplotlib.pyplot as plt
import pandas as pd

data = {
    'Time': [0, 2.5, 5, 7.5, 10],
    'Depth_63.5': [161, 143, 134, 147, 163],
    'Depth_64.5': [183, 190, 375, 255, 241],
    'Depth_65.5': [711, 727, 914, 756, 747],
}

df = pd.DataFrame(data)
depth_cols = [col for col in df.columns if 'Depth_' in col]
# 提取并转换深度值为数值类型
depth_values = [float(col.split('_')[1]) for col in depth_cols]

fig, ax = plt.subplots(figsize=(10, 6))

# 遍历每个深度,绘制时间轨迹,标注振幅
for depth_val, col in zip(depth_values, depth_cols):
    ax.plot(df['Time'], [depth_val]*len(df), marker='o', linestyle='-', label=f'Depth {depth_val}')
    # 在每个时间点标注对应振幅
    for t, amp in zip(df['Time'], df[col]):
        ax.text(t, depth_val, str(amp), ha='center', va='bottom', fontsize=9)

# 配置轴属性
ax.set_title('Time vs Depth (Amplitude Annotated)')
ax.set_xlabel('Time (us)')
ax.set_ylabel('Depth')
ax.set_yticks(depth_values)  # 固定Y轴刻度为实际深度值
ax.legend()
ax.grid(True, axis='x')

plt.tight_layout()
plt.show()

方式2:热力图(颜色体现振幅大小)

将数据重构为二维矩阵,用热力图展示时间、深度与振幅的关系,颜色深浅直观反映振幅差异:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

data = {
    'Time': [0, 2.5, 5, 7.5, 10],
    'Depth_63.5': [161, 143, 134, 147, 163],
    'Depth_64.5': [183, 190, 375, 255, 241],
    'Depth_65.5': [711, 727, 914, 756, 747],
}

df = pd.DataFrame(data)
# 重构数据:深度为行,时间为列
heatmap_data = df.set_index('Time').T
# 将行索引转换为数值型深度值
heatmap_data.index = heatmap_data.index.str.split('_').str[1].astype(float)

fig, ax = plt.subplots(figsize=(10, 6))
# 绘制热力图,显示振幅数值
sns.heatmap(heatmap_data, annot=True, fmt='d', cmap='viridis', ax=ax)

ax.set_title('Time vs Depth vs Amplitude Heatmap')
ax.set_xlabel('Time (us)')
ax.set_ylabel('Depth')

plt.tight_layout()
plt.show()

说明

  • 方式1适合追踪单个深度下振幅随时间的变化趋势,标注清晰展示具体数值;
  • 方式2适合快速对比不同深度、不同时间点的振幅差异,视觉冲击力更强。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 18:13:41