I'm trying to call gunplot from swift to produce some plots in .png
. However, the program below does not work --- the resulting 1.png
file is empty. If I leave the "set term aqua" though, it does call the aqua window with the plot in it. However when I try to set output to file (png or pdf) the result is always an empty file.
The gnuplot commands are ok --- I can correctly run them manually.
import Foundation
let task = NSTask()
task.launchPath = "/opt/local/bin/gnuplot"
task.currentDirectoryPath = "~"
let pipeIn = NSPipe()
task.standardInput = pipeIn
task.launch()
let plotCommand: NSString =
"set term png\n" +
"set output \"1.png\"\n"
"plot sin(x)\n" +
"q\n"
pipeIn.fileHandleForWriting.writeData(plotCommand.dataUsingEncoding(NSUTF8StringEncoding)!)
I'm new both to swift and pipes, so please tell me if there is something wrong or unrecommended with the code.