3.5 How to make Figures with Gnuplot 91
set terminal pslatex
set output "derivative.tex"
set xrange [-15:0]
set yrange [-10:8]
set xlabel "log$_{10}(h)$"
set ylabel "$\epsilon$"
plot "out.dat" title "Relative error" w l
generates aLATEXfilederivative.tex. Alternatively, you could write the above commands in
a filederivative.gnuand useGnuplotas follows
gnuplot>load ’derivative.gnu’
You can then include this file in aLATEXdocument as shown here
\begin{figure}
\begin{center}
\input{derivative}
\end{center}
\caption{Log-log plot of the relative error of the second
derivative of $e^x$ as function of decreasing step
lengths $h$. The second derivative was computed for
$x=10$ in the program discussed above. See text for
further details\label{fig:lossofprecision}}
\end{figure}
Most figures included in this text have been generated using gnuplot.
Many of the above commands can all be baked in a Python code. The following example
reads a file from screen withxandydata, and plots these data and saves the result as a
postscript figure.
#!/usr/bin/env python
importsys
fromNumericimport*
importGnuplot
g = Gnuplot.Gnuplot(persist=1)
try:
infilename = sys.argv[1]
except:
print"Usage of this script", sys.argv[0],"infile", sys.argv[1]; sys.exit(1)
Read file with data
ifile = open(infilename,'r')
Fill in x and y
x = [] ; y = []
forlineinifile:
pair = line.split()
x = float(pair[0]); y = float(pair[1])
ifile.close()
convert to a form that the gnuplot interface can deal with
d = Gnuplot.Data(x, y, title='data from output file', with='lp')
g.xlabel('log10(h)')# make x label
g.ylabel('log10(|Exact-Computed|)/|Exact|')
g.plot(d) # plot the data
g.hardcopy(filename="relerror.ps",terminal="postscript", enhanced=1, color=1)