You need to enable JavaScript to run this app. 如何用Python Matplotlib绘制不同x位置处的u(x,y)流体速度函数曲线
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python Matplotlib绘制不同x位置处的u(x,y)流体速度函数曲线

实现方案

你可以通过循环遍历选定的x位置索引,依次绘制对应位置的u随y变化的曲线即可,以下是两种常用实现方式:

方式1:同坐标轴叠加显示(带图例)

import numpy as np
import matplotlib.pyplot as plt

# 你的基础数据定义
nx,ny = 101,101
x = np.linspace(0,20,nx)
y = np.linspace(0,3,ny)
u = np.random.randn(nx,ny)

# 选定要绘制的x位置对应的索引,可按需修改
x_draw_idx = [0, 10, 20, 30, 40, 50]

plt.figure(figsize=(7,5))
for idx in x_draw_idx:
    # 取出对应x位置的所有y方向的u值
    u_profile = u[idx, :]
    # 绘制曲线,标注对应x值
    plt.plot(u_profile, y, label=f'x = {x[idx]:.1f}')

plt.xlabel('x方向速度u')
plt.ylabel('y坐标')
plt.title('不同x位置的速度剖面')
plt.legend()
plt.grid(alpha=0.3)
plt.show()

方式2:偏移错开显示(避免曲线重叠,适配示例效果)

如果不同x位置的u数值接近,曲线叠加会看不清,可以给每个剖面增加固定偏移量错开显示:

x_draw_idx = [0, 10, 20, 30, 40, 50]
offset = 1.5 # 偏移量大小可根据u的数值范围调整

plt.figure(figsize=(9,5))
for i, idx in enumerate(x_draw_idx):
    u_profile = u[idx, :]
    # 叠加偏移量
    plt.plot(u_profile + i * offset, y)
    # 每个剖面旁标注对应x值
    plt.text(u_profile.mean() + i * offset, 3.1, f'x={x[idx]:.1f}', ha='center')

plt.xlabel('u(含偏移量)')
plt.ylabel('y坐标')
plt.title('不同x位置的速度剖面(偏移错开)')
plt.grid(alpha=0.3, axis='y')
plt.show()

注意事项

如果运行后发现数据和你之前的等高线图不匹配,说明矩阵维度顺序存在差异,将u[idx, :]替换为u.T[idx, :]即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 10:03:02