# Too many contour labels in contour plots

I am trying to create a contour plot with labels in the contour lines. The data is imported from Octave, and stored in a file called data.dat. It contains many contour lines, so that manual modification of the file is not practical.

The following code reproduces the problem:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
table {
0 2.875 5
3 2.875 5

0 2.825 4
3 2.825 4

0 2.75 3
3 2.75 3

0 2.5 2
3 2.5 2

0 1 1
3 1 1
};
\end{axis}
\end{tikzpicture}
\end{document}


The result has some contour lines too close to each other and therefore their labels overimpose. I would like to automatically limit the number of labels that are added to the contour lines in two ways:

1. Those labels with greater value than certain number will not appear. In my example, for example, show only labels in contour lines with value lower than 3.
2. Show labels only for selected contour lines. For example, remove the label that equals to 4.

Any hints about what options to play with are welcome, as I could not find a proper way to do this in the documentation.

-
Welcome to TeX.sx! It is better to provide a full Minimal Working Example (MWE) than a code snippet. An MWE is a complete compilable doc that includes the features necessary for the question. –  Peter Jansson Jan 17 at 17:26

Pgfplots currently only supports to activate all or none, and to modify the distance of adjacent labels on the same curve.

You can, however, change how such labels are to be generated using the label node code. This requires a little bit knowledge how to compare values.

A potential solution could be

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
contour/label node code/.code={%
% Possibility 1: use the *transformed* point meta
% (i.e. relative: 0 = smallest value, 1000=largest)
%
% -> use this to get the first 50% of the labels:
\ifdim\pgfplotspointmetatransformed pt<500pt
\node {\pgfmathprintnumber{##1}};
\else
% Possibility 2: use the *original* point meta
% -> it is in float representation.
%
% -> use this to get specific labels.
\pgfmathfloattofixed{\pgfplotspointmeta}%
%
% \ifdim works only if the |\pgfmathresult| < 16000
% and not too small.
\ifdim\pgfmathresult pt=5pt % get label "5"
\node {\pgfmathprintnumber{##1}};
\fi
\fi
},
contour prepared,
]
table {
0 2.875 5
3 2.875 5

0 2.825 4
3 2.825 4

0 2.75 3
3 2.75 3

0 2.5 2
3 2.5 2

0 1 1
3 1 1
};
\end{axis}
\end{tikzpicture}
\end{document}


I chose to use \ifdim here which expects two dimension arguments (that's why I added "pt" as suffix, that is suitable here). There are other methods to compare numeric values, but I guess this does the job here.

I always wanted to add more predefined options to control contour label placement. Perhaps I should take your question as feature request and do it eventually..

-
Thanks Christian. –  user24528 Jan 21 at 15:33
In fact, following your code we could easily print labels within a certain range by nesting "\ifdim" as needed. For example, to print the labels less than 4 and larger than 1 we would use \ifdim\pgfmathresult pt<4pt and then inside this if another with \ifdim\pgfmathresult pt>1pt, with the final code printing the label \node {\pgfmathprintnumber{##1}};. I think this would be a nice candidate for a predefined control option ;). –  user24528 Jan 21 at 16:10