[Tex/LaTex] pgfplots + gnuplot: how to clean up the look of a contour plot generated from raw gnuplot data by using a colour bar

gnuplotpgfplots

Here's a MWE:

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[view={0}{90}, clip = false, xmin = -2, xmax = 2, ymin = -2, ymax = 2, grid=major, xlabel=$x$, ylabel=$y$]

            \addplot +[no markers,
            raw gnuplot,
            contour prepared,
            ] gnuplot {
                set samples 50, 50;
                set isosamples 51, 51;
                set contour base;
                set cntrparam levels incremental -4,0.25,4;
                set style data lines;
                splot [-2:2] [-2:2] 0.2*x**5 - 0.25*x**4 + 0.5*y**2;
            };

        \end{axis}
    \end{tikzpicture}
\end{document}

It's output has labels that are very messy:

enter image description here

I have an idea on how I can clean it up: use a colorbar, where the line colour is determined by its contour plot level (so, not its y-axis value). Looking through the pgfplots manual, it wasn't obvious to me how I could select the value that determines the colour to be the contour plot z-value (edit: well, point meta seems to be the way, but that's all I know so far). How can I do this?

The following question and its answer seems relevant, if I could figure out what's going on: Mapped grid color for pgfplots colorbar

Best Answer

I don't know why, but colorbar automatically accesses the z-values (I guess, the meta points that come along with the contour plots?). So, turning off the labels, and using the colour bar commands as usual gets things to work as I want:

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[view={0}{90}, clip = false, xmin = -2, xmax = 2, ymin = -2, ymax = 2, grid=major, xlabel=$x$, ylabel=$y$, colorbar, point meta min = -4, point meta max = 3]

            \addplot +[no markers,
            raw gnuplot,
            contour prepared, contour/labels=false,
            ] gnuplot {
                set samples 50, 50;
                set isosamples 51, 51;
                set contour base;
                set cntrparam levels incremental -4,0.25,4;
                set style data lines;
                splot [-2:2] [-2:2] 0.2*x**5 - 0.25*x**4 + 0.5*y**2;
            };

        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

I hope someone can explain more!

Related Question