node.js - 使用 gnuplot 在 Node js 中绘制和保存图形

标签 node.js gnuplot child-process

我在 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/

相关文章:

Gnuplot:如何绘制 45 度线?

rust - 如果我的应用程序崩溃,我如何确保派生的子进程被终止?

javascript - 重新排列对象数组的更有效方法?

gnuplot - 如何将 gnuplot polar 限制在 180 度范围内?

node.js - 在 Node.js 中解析数据

emacs - 如何使用 emacs 允许在长 gnuplot 命令中进行注释?

io - 相当于 Python 的 subprocess.communicate 在 Rust 中?

node.js - 'child_process'.exec 与 .spawn ENOENT

javascript - 如何仅在现有 Node 项目的一部分(购物车页面)中添加/使用 React 组件?

angularjs - 单独的用户/管理员 API?