# Incorrect plot with pgfplots and gnuplot

I'm attempting to plot atanh(x) although there seems to be a giant straight line linking the top and bottom points. Am I doing something wrong?

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[no markers,samples=1001]
\end{axis}
\end{tikzpicture}
\end{document}


-
You should probably limit the domain to avoid winding back which seems the case here. –  percusse Apr 15 '12 at 10:06
Indeed, adding ,domain=-0.99:0.99 to the options helps. –  percusse Apr 15 '12 at 10:18
@percusse Please make that an answer. –  Joseph Wright Apr 15 '12 at 10:43
@JosephWright Thanks for the reminder, done. –  percusse Apr 15 '12 at 12:06

A – perhaps unsatisfactory – solution could be to restrict the y values to a certain domain.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
no markers,
samples=1000,
restrict y to domain=-2:2
]
\end{axis}
\end{tikzpicture}
\end{document}


Derived from Section 4.21 of the »pgfplots« manual (p. 272).

-
Thanks, this is what I did –  Harrison Apr 16 '12 at 5:24

I think it's not a problem with gnuplot because tkz-fct uses gnuplot and I get

with

\documentclass{standalone}
\usepackage{tkz-fct}
\begin{document}
\begin{tikzpicture}
\tkzInit[xmin=-1,xmax=1,xstep=.2,
ymin=-5,ymax=5,ystep=1]
\tkzGrid[color=brown,sub,subxstep=.1](-1,-5)(1,5)
\tkzAxeXY
\tkzFct[color=red,samples=1001,domain = -1:1]%
{atanh(\x)}
\end{tikzpicture}
\end{document}


Perhaps there is an option in pgfplots to avoid this problem. The author of pgfplots Christian F. will surely help you.

-
gnuplot marks the point 0,0 as invalid in the .tkzfct.table (and in the .pgf-plot.table) with an i in the third column. It seems that tkz-fct (and plain TikZ/PGF) uses that column to exclude the point from the plot, while pgfplots does not. –  Jake Apr 15 '12 at 10:46

Here is a comparison of different ways of doing the same job with the resulting tables as Jake commented:

First, let's draw a few atanh(x) plots

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[no markers,samples=202,restrict y to domain=-2:2]
\end{axis}

\begin{scope}[domain=-2:2,shift={(10,2.5)}]
\draw[ultra thick,red] plot[raw gnuplot,id=raw-example] function{set samples 202; plot atanh(x)};
\end{scope}

\begin{scope}[shift={(0,-7)}]
\begin{axis}[no markers,samples=202,domain=-0.999:0.999]
\end{axis}
\end{scope}
\end{tikzpicture}

\end{document}


What I wan't to emphasize here is along the lines of Jake's comment. If you look at the resulting .table files of the first two methods, most of the sampling points are discarded and even in the original pgfplots part it's drawn with lines to origin. Here is the snapshot from the .table file of the raw-example. u and i are the identifiers that confuses pgfplots and it draws them anyway.

As we can see only a limited number of points are of interest and the rest is either outside the domain of the function -1<x<1 or numerical noise which is the reason why I manually tuned to 202 samples. However in my last example I restrict the domain of the function such that all samples are valid and increases the resolution.

Long story short, it's not a good idea to rely on the plotter's abilities for identifying the domain of the function and filtering them, which would result in dummy sampling points , increased compile time and incerased file sizes for no good reason. Although, gnuplot and pgfplots are marvellous tools, we should help them a little anyway :)

-
Thanks, I learnt a lot from your answer –  Harrison Apr 18 '12 at 4:12