我在 Node js 的 bash 子进程中使用 gnuplot 来绘制一些数据并保存图形。这是我到目前为止的代码:
var term = require('child_process').spawn('bash');
term.stdin.write('gnuplot -p\n');
term.stdin.write('set key outside\n');
term.stdin.write('set term png\n');
term.stdin.write('set output "plot1.png"\n')
term.stdin.write('plot for [col=2:4] "results" using 1:col with points title columnheader\n');
term.stdin.write('set output "plot2.png"\n')
term.stdin.write('plot for [col=5:7] "results" using 1:col with points title columnheader\n');
这是更正确、最快的方法吗?无论如何我可以让它更紧凑和简单吗?另外,当我在 node.js 脚本中运行此脚本时,当我运行脚本时,子进程不会退出,因此脚本不会退出。如何解决这个问题?
最佳答案
解决方案:
直接生成 gnuplot var term = require('child_process').spawn('gnuplot');
而不是 bash。
remove term.stdin.write('gnuplot -p\n');
(-p 标志使 gnuplot 窗口持久化,但没有必要这样做,因为在这种情况下,绘图将打印为 png图片)
在末尾添加 term.stdin.write('exit\n')
。
关于node.js - 使用 gnuplot 在 Node js 中绘制和保存图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426422/