# How to plot a third variable (z) on pgfplot as a color gradient

I'm trying to use pgfplots to create a 2D plot with 3 variables, the third one being plotted as a color gradient.

\begin{tikzpicture}[scale=0.75]
\begin{axis}[xlabel=Selta,ylabel={Hitastig $[\si{\celsius}]$},colorbar,colormap/greenyellow]
table[x=S,y=POT_T,z=DB, col sep=semicolon]{287.csv};
\legend{Stöð 287}
\end{axis}
\end{tikzpicture}


I've been looking all over in the pgfplots manual, but I have somehow overlooked how to plot the z variable as the color gradient, this code automatically assumes the y variable is to be plotted.

It always seems problems like these are something incredibly easy that was overlooked, so I hope this will be solved in no time.

Edit:

Although I found the answer to this specific problem, there is another very related matter I'd like an answer to, mods correct me if this should be a separate question.

I'd like to reverse the colorbar, and have found the code to do so is to put colorbar style={z dir=reverse} into the axis options. But as per the previous answer I can't use z as the variable name (or can I?) and pgfplots doesn't accept colorbar style={point meta dir=reverse}.

How would this be achieved, or how to answer the previous question in a way that lets you reverse the colorbar?

-

Adding to your answer: You can "fake" the reversed colorbar by rotating it and putting the labels on the other side. A colorbar is essentially another axis, so all the styles you get with normal axes can be used.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[scale=0.75]
\begin{axis}[colorbar,colormap/cool,
colorbar style={xshift=1cm,
yticklabel pos=left,
yticklabel style={anchor=west},
rotate=180}]
\addplot[scatter,point meta=explicit,ultra thick,point meta min={1}, point meta max={700}]
table[x=S,y=POT_T, meta=DB, col sep=semicolon]{
S;POT_T;DB
0;0;650
1;0.5;400
2;2;150
};
\end{axis}
\end{tikzpicture}

\end{document}


-

Concerning the reversed color bar: Jakes answer is quite good here, but you can also use the same argument with a more "built-in" key: You can also supply colorbar style={y dir=reverse} -- and it should automatically adjust all the remaining stuff. Perhaps the more intuitive key point meta dir=reverse should be added.

Keep in mind that this requires \pgfplotsset{compat=1.3} or newer.

-
Would you mind taking a look at this question about column charts in pgfplots to see whether you can come up with a more elegant solution than I did? tex.stackexchange.com/q/13627/2552 (Sorry for abusing the site's comment function, but I didn't know how else to contact you) –  Jake May 2 '11 at 19:57
@Jake I'll take a note on my todo list for that item, thanks for the hint –  Christian Feuersänger May 10 '11 at 20:59

This answers the original part of the question:

I finally found out what I have to do. Seems like I have to name the third variable meta and add meta point=explicit to the addplot options. I also see that it's possible to add your own range to the colorbar by adding point meta min={value} and point meta max={value} and get something like this code

\begin{tikzpicture}[scale=0.75]
\begin{axis}[xlabel=Selta,ylabel={Hitastig $[\si{\celsius}]$},colorbar,colormap/cool]
\addplot[scatter,point meta=explicit,ultra thick,point meta min={1}, point meta max={700}]
table[x=S,y=POT_T, meta=DB, col sep=semicolon]{287.csv};
\legend{Stöð 287}
\end{axis}
\end{tikzpicture}


P.S. Is it ok to answer your own question like this when you finally find the answer?

-
Yes, it's perfectly reasonable to answer your own question, and even to accept it, I think, though I imagine you don't get the points... –  Brent.Longborough Mar 27 '11 at 16:00
It is even encouraged that you accept your own answer if you get no better answers. However, you'll have to wait 48 hours to accept. (And no, it won't give you reputation.) –  Hendrik Vogt Mar 27 '11 at 16:09
@Hendrik Vogt. I found my answer to be incomplete, I edited the question to reflect what I feel is missing, but should I let the answer stay, as it is incomplete? –  Jóhann Mar 27 '11 at 16:22
You can edit your answer to tell which part of the question it addresses. –  Hendrik Vogt Mar 27 '11 at 16:40