3

I want to add a legend to a pgfplots contour plot that is generated using the contour prepared option. The legend should contain the colors of the various levels and show the value associated with a given level. This post suggests a way that originally did not work (in 2013) but I assume should work in the current version of pgfplots. However, the output only lists one legend entry and not two.

Here is the MWE:

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,
  legend image post style={
    sharp plot,
    draw=\pgfkeysvalueof{/pgfplots/contour/draw color},
  },
  legend entries={foo,bar}]
  \input{data.tex}
\end{axis}
\end{tikzpicture}
\end{document}

The file data.tex contains the following:

\addplot[contour prepared={labels=false}]
table {
 0.1 0.9 1.0
 0.9 0.2 1.0

 0.1 0.8 0.1
 0.9 0.1 0.1
};

The data is contained in a separate file because in my real application the isolines are computed by a separate code. I kept it like this because I don't know whether this will make a difference as far as the legend is concerned. The output of the two files above is:

enter image description here

So there appear to be two problems:

  1. It only shows "foo".
  2. The color is black and not red.

What I would like to get is (produced with plot table, so not a contour plot):

enter image description here

1
  • Do you want to use the contour prepared option or not? I have a solution without that option, but your question seems ambiguous on this point.
    – Alenanno
    Commented Jan 1, 2016 at 15:02

1 Answer 1

3
+50

Here are two ways, both using contours prepared, the first one with automatic colors, the second one with explicitly defined colors.

Contour prepared (automatic colors)

enter image description here

The data.tex file looks like this. Adding the + solved the issue when using color list name

\addplot+[contour prepared={labels=false}]
table {
    0.1 0.9 1.0
    0.9 0.2 1.0
};
\addplot+[contour prepared={labels=false}]
table {
    0.1 0.8 0.1
    0.9 0.1 0.1
};

And here's the code:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,
    cycle list name=color list,
    legend entries={foo, bar}
  ]

\input{data.tex}
\end{axis}
\end{tikzpicture}
\end{document}

Contour prepared (with explicit colors)

enter image description here

The data.tex file is as follows:

\addplot[contour prepared={draw color=red,labels=false}]
table {
 0.1 0.9 1.0
 0.9 0.2 1.0
};
\addplot[contour prepared={draw color=blue,labels=false}]
table {
 0.1 0.8 0.1
 0.9 0.1 0.1
};

Then the file will contain:

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,
    legend image post style={
        sharp plot,
        draw=\pgfkeysvalueof{/pgfplots/contour/draw color},
    },
    legend entries={red, blue}
  ]

\input{data.tex}
\end{axis}
\end{tikzpicture}
\end{document}
6
  • I'm only interested in the contour prepared option. If I understand your answer correctly, the only things you changed are the format of the data.tex file by separating each contour line into a separate table and explicitly adding the color. This goes a little beyond what I was hoping for since I do not want to explicitly have to specify the color of each contour line. Instead, I would prefer keeping the pgfplots contour colors and simply having a legend that shows the value of all the contour lines together with the colors. Commented Jan 1, 2016 at 19:32
  • @user1362373 So, you don't have a problem with the separation of the plots, but you want automatic colors for the plots?
    – Alenanno
    Commented Jan 1, 2016 at 19:44
  • I would prefer for data.tex to remain untouched. If that is not possible, I can live with each contour line having its own table entry. I want the legend to be generated automatically so it looks like the second figure I put into my question. Commented Jan 1, 2016 at 20:21
  • @user1362373 Yes, the fact is that in order to have their own entry in the legend, you need to assign them their own plot. This is the reason why you only had one entry in the legend box: it thought it was a single plot. About your second figure, are you also referring to the entries 1.0 and 0.1?
    – Alenanno
    Commented Jan 1, 2016 at 20:27
  • @user1362373 By the way, the question you linked to shows exactly the solution I provided, explicitly defining the color for each plot. Using color list name=color works, but only without contour prepared. And actually even without defining this, you'll get automatic colors in this last case.
    – Alenanno
    Commented Jan 1, 2016 at 20:33

You must log in to answer this question.

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