# GnuplotTex: automatic plotting and vertical line indication

I am using pdflatex with the gnuplottex package, to plot a function with a local maximum; I'd like to find this maximum automatically and label it.

For the most part, I'm already there (using the special "pseudo" file '+' in gnuplot >4.4), apart from a couple of problems -- and I'm not sure whether the problem is in LaTeX or gnuplot ... Here is a minimal working example:

% build with:
% pdflatex -shell-escape test.tex

\documentclass{article}
\makeatletter\newwrite\verbatim@out\makeatother
\usepackage{gnuplottex}

\begin{document}

\section{Test}

Here a brief test...

\begin{figure}[h]
\centering
\begin{gnuplot}[scale=0.95]
# Define helper functions
ismax(x) = (x>max)?max=x:0
isxmax(x) = (ismax(f(x))!=0)?xmax=x:0 #

# Initialise the 'global' vars
max=-1e38
xmax=-1e38
min=1e38

set grid
set title 'gnuplottex test'
set ylabel '$y$'
set xlabel '$x$'
set xrange [0:2000]
set yrange [0:ymaxrange] # MUST set this!

# define the function
f(x) = (20*x)/(100000 + 50*x + x**2)

set multiplot

# plot f(x) # OK, works as usual

# to turn off the annoying label in the upper right corner - also f($0) will cause latex crash set nokey # plot the function - which will also calculate xmax (first pass) plot '+' using ($1):(f($1)) with linespoints, \ '+' using ($1):(isxmax($1)) with lines linecolor 2 # '+' using ($1):(ismax(f($1))) with lines linecolor 2 # second part of plot - which needs xmax set grid noxtics noytics # prevent double plot ?! set arrow from xmax,0 to xmax,3 nohead lt 1 linewidth 2 # linewidth doesn't change ?! set label "X" at xmax,f(xmax) set label "(%.0f;",xmax,"%f)",f(xmax) at 0.6*xmax,f(xmax)+ymaxrange/10 plot '+' using ($1)
# replot # nope, doubles
unset multiplot
\end{gnuplot}
\end{figure}

500 1000 1500 ... End of test.

\end{document}


This code results with a rendering like this (which is a screenshot of evince rendering the PDF):

And these are my problems:

• It seems that upon execution of the second plot, the labels and axes get repeated on top of each other, in spite of a set grid noxtics noytics (notice they are a bit darker in the screenshot) -- can this be prevented?
• In principle, without the set yrange ... line, the second plot may contain a different automatic range (though that is not visible in this example). Is there a way to "copy"/duplicate a range of an axis that was computed automatically in gnuplot?
• the linewidth (lw) argument of set arrow (which is for implementing a vertical line) seems not to have any effect -- how can I manipulate that?
• The line from set arrow and the (very thin dotted) line from the isxmax(\$1) plot do not match; seemingly it is the arrow that is off -- how to fix this?
• The linecolor argument seems to have no effect -- how to fix this? (btw, color seems to work fine for \begin{gnuplot}[terminal=pdf,..., however, I'd like to keep the LaTeX 'terminal')

References:

-

## migrated from stackoverflow.comApr 28 '11 at 9:15

This question came from our site for professional and enthusiast programmers.

I would use the pgfplots package for this. You can generate the data with gnuplot by using \addplot gnuplot {<expression>};, and then read the generated data using \pgfplotstableread{\jobname.pgf-plot.table}\table. After sorting this table, you just access the first element, which now contains the local maximum.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}

\begin{tikzpicture}
\begin{axis}[ymin=0,ymax=0.05,xmin=0,xmax=2000,grid=both]

gnuplot {(20*x)/(100000 + 50*x + x**2)};

\pgfplotstablesort[sort cmp={float >},sort key={[index] 1}]\sorted{\table}
\pgfplotstablegetelem{0}{[index] 1}\of{\sorted}
\let\maxy=\pgfplotsretval
\pgfplotstablegetelem{0}{[index] 0}\of{\sorted}
\let\maxx=\pgfplotsretval

\node at (axis cs:\maxx,\maxy)
[circle, fill, red,inner sep=1.5pt,
pin={
[fill=white]40:{(\pgfmathprintnumber{\maxx}, \pgfmathprintnumber{\maxy}})
}
] {};
\draw [red] (axis cs:\maxx,0) -- (axis cs:\maxx,1);
\end{axis}
\end{tikzpicture}

\end{document}


-
Hi @Jake - thanks a lot for this answer! It seems like the optimal solution that will utilize the gnuplot syntax... –  sdaau May 8 '11 at 19:21

Many of your questions concern how GnuPlot constructs graphs and don't have much to do with TeX. Unless there are some GnuPlot experts around here, more insight may be found on their mailing list:

As for not getting colored output from the latex terminal---that question I can answer: The GnuPlot latex terminal does not include any color information into the picture code that it generates. You need to use a different terminal.

### The epslatex terminal:

\begin{gnuplot}[scale=0.95,terminal=epslatex,terminaloptions=color]


If using pdflatex, you will also need to convert the resulting EPS files to PDF. This can be done by installing the epstopdf tool and adding the following to your preamble:

\usepackage[suffix=]{epstopdf}


However, feeding EPS to pdflatex just feels like a hack. There are other GnuPlot terminals you can use to get around this.

### The tikz terminal:

\begin{gnuplot}[scale=0.95,terminal=tikz,terminaloptions=createstyle]


You also need to change the upper y value on your arrow from:

set arrow from xmax,0 to xmax,3 nohead lt 1 linewidth 2


To:

set arrow from xmax,0 to xmax,ymaxrange nohead lt 1 linewidth 2


Or it will shoot off into space.

The following has to be added to the preamble of your document:

\makeatletter
% Tell gunuplottex to bring TikZ output in using \input rather
% than \includegraphics
\def\gnuplottexextension@tikz{\string tex}
\makeatother
\usepackage{tikz}
\usepackage{gnuplot-lua-tikz} % Generated by the createstyle option


The results are:

Other GnuPlot terminals that may be of interest generate Metapost and PS-Tricks output.

-
Hi @Sharpie, thanks a lot for the detailed color and terminals explanation! I guess, I wanted to use a gnuplot syntax for function plotting, along with calculation of a max value - and yet have the whole thing coded in a Latex file (i.e. without intermediary image files) ... Cheers! –  sdaau May 8 '11 at 19:24