9

When setting an axis limit in TikZ datavisualization, the data outside the axis gets drown and overlays the axis, also pushing the picture bounding box size. How can I make it clip to the axis bounds?

\documentclass[tikz,border=2mm]{standalone}
 \usetikzlibrary{datavisualization}
\begin{document}
\begin{tikzpicture}

 \datavisualization [%
    scientific axes,
    y axis={min value=-1},
    visualize as line
  ]
  data { 
    x, y
    0, 0
    1, -0.5
    2, -2
    3, -0.5
    4, 0
};

\end{tikzpicture}
\end{document}

Preview

3 Answers 3

6

Datavisualization

I checked the documentation (Tikz Manual, VI. Data Visualization, page 851 in version 3.1.9a), but I couldn't find anything about this. So the only way would be for you to change the min value to the minimum you have in your coordinates.

This would yield this result:

figure 1

It's not quite what you have above, but there was no mention of ways to clip or constrain the domain to certain coordinates.

Pgfplots

Alternatively you could reproduce the same plot using pgfplots which, apparently, is better at controlling what is displayed. For example, in your case, you could write domain=-1:4 and anything beyond those coordinates would be clipped. This is how your graph would appear:

figure 2

Code (for both pictures)

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}

\usetikzlibrary{datavisualization}

\begin{document}
\begin{tikzpicture}

 \datavisualization [%
    scientific axes,
    y axis={min value=-2},
    visualize as line,
  ]
  data { 
    x, y
    0, 0
    1, -0.5
    2, -2
    3, -0.5
    4, 0
};
\begin{scope}[xshift=8cm]
\begin{axis}[
    height=4.7cm,
    width=7cm,
    xtick align=outside,
    ytick align=outside,
    x tick label style={font=\footnotesize},
    y tick label style={font=\footnotesize},
    enlargelimits=false,
    ymin=-1
    ]
    \addplot[mark=none,line width=.6pt, domain=-1:4] coordinates{(0,0) (1,-0.5) (2,-2) (3,-0.5) (4,0)};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{document}
1

Here is the way how we can do this with mathematical expressions. You should create a new format (Chapter 76.6 "Advanced: Defining New Formats" Till's Tantau 2015 "The Tik Z and PGF Packages" manual).

While parsing lines from the input file the pattern evaluates each first value in the line and compares it to the range borders "\rmin" and \rmax". The result of logical operation is stored in "\pgfmathresult" that is used by "\ifnum" to make decision of either adding it to the data points array or skipping the whole line.

Code:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\pgfdeclaredataformat{inrange}
{}
{}
{#1 #2}
{
\def\rmin{-2.0}
\def\rmax{2.0}
\pgfmathparse{and(greater(#1,\rmin),notgreater(#1,\rmax))}
\ifnum\pgfmathresult=1
\pgfkeyssetvalue{/data point/x}{#1}
\pgfkeyssetvalue{/data point/y}{#2}
\pgfdatapoint
\else
\relax
\fi
}
{}
{}

\begin{document}
\begin{tikzpicture}[scale=1.0]
\datavisualization [scientific axes,
                    visualize as smooth line, 
                    x axis={min value=-2.2, max value=2.2},
                    y axis={min value=0, max value=5, ticks=few}]
data  [format=inrange,
read from 
file=snippet.dat ];
\end{tikzpicture}
\end{document}

Contents of file "snippet.dat":

-3.0 2.3
-2.3 5.6
-1.6 1.3
-0.4 0.3
0.8 1.5
1.7 2.3
2.4 1.6
2.8 1.8
3.3 3.7

Result (only points within [-2.0;2.0] are used):

Only points within [-2.0;2.0] are used

1

Install a clipping path before invoking \datavisualization,

\documentclass[tikz,border=2mm]{standalone}
 \usetikzlibrary{datavisualization}
\begin{document}
\begin{tikzpicture}

\clip(-18bp,-8bp) rectangle (5cm+2bp,0.618*5cm);
\datavisualization [%
    scientific axes,
    y axis={min value=-1},
    visualize as line
  ]
  data {
    x, y
    0, 0
    1, -0.5
    2, -2
    3, -0.5
    4, 0
};

\end{tikzpicture}
\end{document}

enter image description here

Above, the clipping path is set such as to appreciate that the tick labels, possibly axes labels and data is clipped, but not the axis lines or axis ticks. To complete the example, one would adjust the clipping path to the axis box and, e.g., draw the axis a second time,

\begin{tikzpicture}

\begin{pgfscope}
\clip(0,0) rectangle (5cm,0.618*5cm);
\datavisualization [%
    scientific axes,
    y axis={min value=-1},
    visualize as line
  ]
  data { 
    x, y
    0, 0
    1, -0.5
    2, -2
    3, -0.5
    4, 0
};
\end{pgfscope}
\datavisualization[scientific axes,
    y axis={include value=-1, include value=0},
    x axis={include value=0, include value=4}]
data {};

\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .