gnuplot读取自生成PNG实现叠加时报错的技术求助
解决gnuplot叠加PNG图像的报错问题
问题核心
你要在同一个gnuplot会话里叠加自生成的PNG图实现无背景层叠,但最后一步plot命令触发报错,本质是直接plot PNG文件时,gnuplot默认将其当作数值数据解析,而非图像文件处理,再加上初始生成的PNG没有透明背景,也无法实现真正的层叠效果。
报错信息
line 0: Bad data on line 1 of file [...] line 0: warning: using default binary record/array structure line 0: Too many using specs for this style
修正方案
方案1:正确用gnuplot读取PNG并叠加
步骤1:生成带透明背景的PNG
旧版png终端不支持透明,必须改用pngcairo终端并开启透明属性,否则叠加时背景会覆盖下层图像:
set terminal pngcairo transparent size 600,600 set size ratio -1 set nokey set noxtics set noytics set noborder set parametric i2p = {0,1}*2*pi t0 = 0 t1 = 1 # 生成第一个透明背景图像 test01(t) = exp(i2p*(2*t)) set output "solve_png_problem_15nov22a.png" plot [t=t0:t1] real(test01(t)), imag(test01(t)) lc 1 # 生成第二个透明背景图像 test02(t) = 3*1.0**20 * exp(i2p*(-3*t+20/200. )) + 3*1.0**19 * exp(i2p*(2*t+20/200.)) set output "solve_png_problem_15nov22b.png" plot [t=t0:t1] real(test02(t)), imag(test02(t)) lc 2
步骤2:正确执行叠加操作
叠加时必须指定binary filetype=png with rgbimage,同时要保证图像尺寸、坐标范围和生成时一致,避免错位:
set terminal pngcairo size 600,600 set output "solve_png_problem_15nov22_overlay.png" # 匹配生成图像时的坐标范围,确保对齐 set xrange [-3:3] set yrange [-3:3] set size ratio -1 plot 'solve_png_problem_15nov22a.png' binary filetype=png with rgbimage, \ 'solve_png_problem_15nov22b.png' binary filetype=png with rgbimage
方案2:更高效的直接绘制法
完全不需要生成中间PNG文件,直接在同一张图里绘制所有函数,一步到位:
set terminal pngcairo size 600,600 set size ratio -1 set nokey set noxtics set noytics set noborder set parametric i2p = {0,1}*2*pi t0 = 0 t1 = 1 set output "solve_png_problem_15nov22_overlay_direct.png" plot [t=t0:t1] real(test01(t)), imag(test01(t)) lc 1, \ [t=t0:t1] real(test02(t)), imag(test02(t)) lc 2
方案3:整合SVG终端的简化操作
SVG是矢量格式,叠加更方便,直接在同一会话绘制多个函数即可,后续如需转PNG,用工具转换:
set terminal svg size 600,600 transparent set output "solve_png_problem_15nov22_overlay.svg" set size ratio -1 set nokey set noxtics set noytics set noborder set parametric i2p = {0,1}*2*pi t0 = 0 t1 = 1 plot [t=t0:t1] real(test01(t)), imag(test01(t)) lc 1, \ [t=t0:t1] real(test02(t)), imag(test02(t)) lc 2
转PNG命令(需安装librsvg2-bin):
rsvg-convert solve_png_problem_15nov22_overlay.svg -o solve_png_problem_15nov22_overlay.png
关键说明
pngcairo终端是实现透明背景的必要条件,旧png终端不支持该属性with rgbimage参数必须添加,用于告知gnuplot将文件当作图像渲染,而非数值数据- 直接绘制多个函数是最高效的方案,避免了中间文件的生成与读取问题
内容的提问来源于stack exchange,提问作者Bryan-StackExchange
相关产品推荐
相关产品推荐

