Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
    \addplot[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}

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.

share|improve this question
2  
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 '13 at 17:26

1 Answer 1

up vote 4 down vote accepted

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

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}
    \addplot[
        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..

share|improve this answer
    
Thanks Christian. –  user24528 Jan 21 '13 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 '13 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.