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

如何在Matplotlib的2D场图上叠加剖面曲线?

在2D场图上叠加剖面曲线的实现方法

要在你现有的2D场图上添加剖面曲线,核心是先标记剖面路径,再通过插值提取剖面上的物理量值,最后绘制剖面曲线。以下是具体实现方案:

方案1:双栏布局(2D场+独立剖面子图)

这种方式将2D场和剖面曲线分为左右两个子图,清晰直观:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import LinearNDInterpolator

def field_with_profile(x, y, z, prof_start, prof_end):
    # 创建1行2列的子图布局
    fig, (ax_field, ax_profile) = plt.subplots(1, 2, figsize=(12, 5))
    
    # 绘制原始2D场
    ax_field.tricontour(x, y, z)
    cntr = ax_field.tricontourf(x, y, z)
    fig.colorbar(cntr, ax=ax_field)
    
    # 在2D场图上标记剖面线(红色粗线)
    ax_field.plot(
        [prof_start[0], prof_end[0]], 
        [prof_start[1], prof_end[1]], 
        'r-', linewidth=2, label='剖面线'
    )
    ax_field.legend()
    ax_field.set_xlabel('X')
    ax_field.set_ylabel('Y')
    
    # 生成剖面上的采样点(均匀取100个点)
    num_sample = 100
    x_prof = np.linspace(prof_start[0], prof_end[0], num_sample)
    y_prof = np.linspace(prof_start[1], prof_end[1], num_sample)
    
    # 对散点数据插值,得到剖面上的物理量Z值
    interpolator = LinearNDInterpolator(list(zip(x, y)), z)
    z_prof = interpolator(x_prof, y_prof)
    
    # 计算剖面上各点到起点的距离(作为剖面曲线的横轴)
    dist_prof = np.sqrt((x_prof - prof_start[0])**2 + (y_prof - prof_start[1])**2)
    
    # 绘制剖面曲线
    ax_profile.plot(dist_prof, z_prof, 'b-')
    ax_profile.set_xlabel('距离')
    ax_profile.set_ylabel('物理量Z')
    ax_profile.set_title('剖面曲线')
    ax_profile.grid(True)
    
    plt.tight_layout()
    plt.show()

方案2:内嵌子图(剖面曲线叠加在2D场图内)

如果希望剖面曲线直接放在2D场图的角落,可使用内嵌子图:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import LinearNDInterpolator
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

def field_with_inset_profile(x, y, z, prof_start, prof_end):
    fig, ax_field = plt.subplots(figsize=(8, 6))
    
    # 绘制原始2D场
    ax_field.tricontour(x, y, z)
    cntr = ax_field.tricontourf(x, y, z)
    fig.colorbar(cntr, ax=ax_field)
    
    # 标记剖面线
    ax_field.plot(
        [prof_start[0], prof_end[0]], 
        [prof_start[1], prof_end[1]], 
        'r-', linewidth=2, label='剖面线'
    )
    ax_field.legend()
    ax_field.set_xlabel('X')
    ax_field.set_ylabel('Y')
    
    # 生成剖面采样点并插值
    num_sample = 100
    x_prof = np.linspace(prof_start[0], prof_end[0], num_sample)
    y_prof = np.linspace(prof_start[1], prof_end[1], num_sample)
    interpolator = LinearNDInterpolator(list(zip(x, y)), z)
    z_prof = interpolator(x_prof, y_prof)
    dist_prof = np.sqrt((x_prof - prof_start[0])**2 + (y_prof - prof_start[1])**2)
    
    # 创建右上角的内嵌子图
    ax_inset = inset_axes(ax_field, width="30%", height="30%", loc='upper right')
    ax_inset.plot(dist_prof, z_prof, 'b-')
    ax_inset.set_xlabel('距离', fontsize=8)
    ax_inset.set_ylabel('Z', fontsize=8)
    ax_inset.tick_params(labelsize=7)
    
    plt.tight_layout()
    plt.show()

使用说明

调用函数时,只需传入你的散点数据x, y, z,以及剖面的起点和终点坐标(比如prof_start=(x_min, y_mid), prof_end=(x_max, y_mid))即可。

关键细节

  • 因为你用tricontour处理的是散点数据,所以用LinearNDInterpolator进行线性插值来获取剖面上的Z值
  • 剖面的采样点数可根据需求调整(示例中是100),点数越多曲线越平滑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 13:54:22