如何用Gnuplot处理并绘制含字符串的第11列数据?
用Gnuplot可视化带字符串注释的列数据
数据示例
1 2 3 … 10 11 1 1.0000000e-06 1.0000000e-06 … 0 2 2.5000000e-06 1.5000000e-06 … 0 #dt_grow 3 4.7500000e-06 2.2500000e-06 … 0 #dt_grow 4 8.1250000e-06 3.3750000e-06 … 0 #dt_cfl 5 1.2450703e-05 4.3257029e-06 … 1 #dt_mach, max_iteration_turbulence 6 1.6811013e-05 0.3603104e-06 … 0 #dt_grow
第11列特点
- 第1行第11列无值
- 所有值均以
#开头,无引号包裹 - 包含多种不同条目(如
#dt_piso、#dt_piso, 2*max_piso reached等) - 当第11列含附加信息时,第10列值非零
- 数据行数约为10^6量级
尝试过的报错命令
# 直接调用第11列,报错x范围无效 p "file" u 1:11 w l # 输出:--> x range is invalid # 尝试字符串拼接判断,报错无有效数据点 p "file" u 1:(''.$11 eq "#dt_cfl" ? 1 : 0) w l # 输出:--> warning: Skipping data file with no valid points. x range is invalid # 用column()调用,报错字符串操作应用于非字符串类型 p "file" u 1:(column(11) eq "#dt_cfl" ? 1 : 0) w l # 输出:--> internal error : STRING operator applied to non-STRING type # 用strcol()调用,同样报错字符串类型不匹配 p "file" u 1:(strcol(11) eq "#dt_cfl" ? 1 : 0) w l # 输出:--> internal error : STRING operator applied to non-STRING type # 错误使用splot,报错需要1或3列数据 splot "time.out" u 1:(11 eq "#dt_cfl" ? 1 : 0) w l # 输出:--> Need 1 or 3 columns for cartesian data # 尝试自定义函数,同样无法解决 f(x)= ''.x eq "#dt_cfl" ? 1 : 0
解决方案
1. 基础字符串映射
Gnuplot中strcol(n)用于提取第n列的字符串,空值会返回空字符串。通过自定义函数将不同字符串映射为数值:
# 定义映射规则,可根据实际条目扩展 map_comment(s) = s eq "#dt_grow" ? 1 : \ s eq "#dt_cfl" ? 2 : \ s eq "#dt_mach, max_iteration_turbulence" ? 3 : \ 0 # 未匹配条目返回0 # 绘制演化曲线,第1列为x轴,映射值为y轴 plot "file" u 1:(map_comment(strcol(11))) w l title "Comment Evolution"
2. 结合第10列附加信息
利用第10列非零的特性,在映射时叠加该列数值,突出特殊状态:
map_with_col10(s, col10) = s eq "#dt_grow" ? 1 : \ s eq "#dt_cfl" ? 2 : \ s eq "#dt_mach, max_iteration_turbulence" ? 3 + col10 : \ 0 plot "file" u 1:(map_with_col10(strcol(11), $10)) w l title "Comment with Col10 Info"
3. 大数据量优化
针对10^6行的数据集,提前导出映射结果可提升绘图效率:
set table "temp_mapped.dat" # 将映射结果写入临时文件 plot "file" u 1:(map_comment(strcol(11))) with table unset table plot "temp_mapped.dat" u 1:2 w l title "Optimized Mapping"
4. 处理带逗号的复杂条目
Gnuplot默认以空白符分割列,逗号属于列内容的一部分,直接用strcol(11)即可完整提取带逗号的字符串,无需额外处理。
内容的提问来源于stack exchange,提问作者Salvatore
相关产品推荐
相关产品推荐

