Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call gnuplot from CLI and save the output graph to the image file?

Tags:

gnuplot

I'm writing a batch file which will also generate a gnuplot graph from a dat file.

I wish to call gnuplot from the command line, using the gnuplot "gnu" script I have written, and save the output graph to an image.

Something like:

gnuplot.exe script.gnu > image.png

Any ideas?

like image 971
Idanis Avatar asked Dec 26 '12 22:12

Idanis


People also ask

How do you save a gnuplot graph?

There are two ways to save your work in gnuplot: you can save the gnuplot commands used to generate a plot, so that you can regenerate the plot at a later time. Or you can export the graph to a file in a standard graphics file format, so that you can print it or include it in web pages, documents, or presentations.

How do I save a gnuplot file as a png?

gnuplot> set term png (will produce . png output) gnuplot> set output "printme. png" (output to any filename. png you want) gnuplot> replot gnuplot> set term x11 You can view it with some viewer, or on a browser, or print it... or import it in PowerPoint or whatever you want to do with it.

How use gnuplot to plot data from a file?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.

How do I save a plot in Linux?

Plots panel –> Export –> Save as Image or Save as PDF Specify files to save your image using a function such as jpeg(), png(), svg() or pdf(). Additional argument indicating the width and the height of the image can be also used.


2 Answers

You don't have to redirect the output from gnuplot into an image file; you can set that inside the gnuplot script itself:

set terminal png
set output 'image.png'

If you want to have a variable output name, one simple way to do that in bash is to wrap the gnuplot commands thus:

#!/bin/bash

echo "set terminal png
set output '$1'
plot 'data.dat'" | gnuplot

This way you can run the bash script with an argument for the output file name:

./plotscript.sh image.png
like image 179
andyras Avatar answered Sep 28 '22 19:09

andyras


Simply putting the following line will make gnuplot to return png-format bytecode. Thus, you can redirect the output to a png-file.

set terminal png
like image 40
Sunhwan Jo Avatar answered Sep 28 '22 19:09

Sunhwan Jo