# PGF/TikZ plot: syntax and limit range of plotted function

In this answer an example of multiple functions plot is provided, using PGF/TikZ and the datavisualization library. The documentation seems not to deal with the following issues:

1) For each plot, the x range is defined as:

var x : interval [-0.5*pi:4];


This way, whatever the function is and whatever values it reaches in the x range, it will be forced to be plot.

Is it possible to also limit the y, that is the plotted range? I trivially tried

func y = tan(\value x r) : interval [0:4];


(to obtain function values no higher than 4 and no lower than 0) but it gives error. Limiting the axis has no effects on the plot.

2) In

func y = sin(\value x r);


what is the final r used for? If I try to remove it, the plot is incorrect. Where can some information about this syntax be found?

• TikZ assumes the arguments of sin etc. to be degrees, and as you can see from the plots, r converts radians to degrees.
– user121799
Feb 23, 2018 at 16:51

UPDATE: A little research reveals that this question has already been asked here, but the answer suggests that it doesn't really work. In more detail, min value=-4 and max value=4 are supported, but they prevent the plot not from overshooting, as mentioned in the pgfmanual in section 77.2.3. I also started to play with the at start survey keys and the like, attempting to clip on the data visualization bounding box. Yet this was not successful. So in the end the only thing I can offer here is the very ad hoc way to confine the tan plot is to plot max(min(tan(\value x r),4),-4) instead, which only takes values between -4 and 4. As I mentioned above, r converts radians into degrees.

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\begin{document}
\begin{tikzpicture}
\datavisualization [scientific axes=clean,
y axis={grid,min value=-4,max value=4},
visualize as smooth line/.list={sin,cos,tan},
style sheet=strong colors,
style sheet=vary dashing,
sin={label in legend={text=$\sin x$}},
cos={label in legend={text=$\cos x$}},
tan={label in legend={text=$\tan x$}},
data/format=function,
%                   at start survey={\clip (data visualization bounding
%                   box.south west) rectangle (data visualization bounding box.north east);}
]
data [set=sin] {
var x : interval [-0.5*pi:4];
func y = sin(\value x r);
}
data [set=cos] {
var x : interval [-0.5*pi:4];
func y = cos(\value x r);
}
data [set=tan] {
var x : interval [0:4];
func y = max(min(tan(\value x r),4),-4);
};

\end{tikzpicture}
\end{document}


Perhaps someone else can make the commented out bit work....

• First of all, thank you for your answer and your attempts. I tried with 4 plots, this is the output. The 3rd plot is func y = max(min(tan(\value x r),4),-4);, the 4th is simply func y = tan(\value x r);. Both the plots seem to have unusual blends around pi/2: the 3rd, an unusual "slow" slope before pi/2; the 4th, an unusual bending. Maybe the tangent itself is not easy to plot with datavisualization. Feb 26, 2018 at 18:26
• @BowPark My impression is (but I may be completely wrong) that pgfplots, which requires TikZ, is a better tool to do these things. The visualization stuff just lays the foundations.
– user121799
Feb 26, 2018 at 19:55
• After some small test plots, I definitely agree. They are different tools, but pgfplot fits more to this kind of task. Feb 26, 2018 at 20:45