1

Sometimes the automatic placement of pgfplots's contour labels is not satisfactory. For example, consider this example from the manual (with minor changes),

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.12}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[domain=-2:2,view={0}{90}]
  \addplot3[contour gnuplot={number=14,label distance=10000pt},thick]
    {exp(0-x^2-y^2)*x};
  \end{axis}
\end{tikzpicture}
\end{document}

which gives

enter image description here

Now I simply change the upper limit of the x-axis to -0.75,

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.12}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[domain=-2:2,view={0}{90},xmax=-0.75]
  \addplot3[contour gnuplot={number=14,label distance=10000pt},thick]
    {exp(0-x^2-y^2)*x};
  \end{axis}
\end{tikzpicture}
\end{document}

I get

enter image description here

Now the contour label positions coincide with the right y-axis and are therefore invisible. Of course, I could increase xmax and/or lower the value of label distance to get more labels. However, in my real problem (not the above MWE) I cannot increase xmax (it represents the maximum value of the independent variable) and I think in general a smaller number of labels looks better. It seems to me that in this case it would be better if the labels were placed either

  1. along y=0 or
  2. along the line (-1,1.6) -- (-0.75,0).

Which brings me to my question: Can I specify manually the position of contour labels and if so, how?

(This may seem crazy when using gnuplot to generate the contours, but in general I use my own tool and the contour prepared option, so I could compute the position of the labels quite easily. The angle should also be possible, though perhaps a little more challenging for poorly resolved contour levels.)

1

Since nobody posted an answer or a comment, I thought I'd briefly outline the work-around I developed. Perhaps it will be useful for somebody although the work-around is not automated.

As alluded to at the end of my question, I amended my code to extract isolines so it can also write out labels. For simplicity, I assume that they are placed at the intersection points of one or more straight lines and the isolines. The amended code writes out a file with the labels as nodes, i.e.,

\node[align=center,fill=white,inner sep=1pt,font=\tiny,rotate=  46.3] at (axis cs: 0.132129E+01,-0.914058E+00) {0.1};

which I then input into the file with the TikZ/pgfplots commands. The angle can be computed automatically from the slope of the isolines where it intersects the straight line with the labels.

For the example function from the pgfplots manual, this is the result:

enter image description here

In my opinion, this looks better than the automated placement of labels by pgfplots, but it is more interactive and requires the use of the contour prepared option. One thing that is missing is the matching of the color of the contour label to that of the contour itself…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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