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'm trying to create a 2D plot with a color map using pgfplots. I've managed to get the overall behaviour that I want, but I would like the color map to be logarithmic, rather than linear. The best I have managed is to use the meta expr key, but then the color bar shows 1-4 instead of 10^1 through 10^4. Below is my working example.

\begin{tikzpicture}
\begin{semilogyaxis}[colorbar,
xlabel = create energy (keV),
ylabel = deposit energy (kev),
]
\pgfplotstableread{../endep_log_au_30_0_0.csv}\mytable %
\addplot+[scatter, only marks, scatter src=explicit, mark=square]
 table[x=createEn,y=depEn,meta expr=ln(\thisrow{tot})/ln(10)] {\mytable};
\end{semilogyaxis}
\end{tikzpicture}

Is there a way to modify how the labels appear on the colorbar or set it to be logarithmic? Is there a different way I should be approaching this problem?

(Unfortunately I cannot post an image as a new user)

share|improve this question
    
Welcome to TeX.sx! It would be good if you could add full minimal working example (MWE) that people can cut and paste to try out the code. In your case, the preamble and \endep_log_au_30_0_0.csv are missing, which makes it harder to help with solving the problem. –  Jake Jul 21 '11 at 23:59

1 Answer 1

up vote 4 down vote accepted

The meta expr approach is a good one. In order to get the labels on the colorbar right, you can set \yticklabel=\pgfmathparse{10^\tick}\pgfmathprintnumber{\pgfmathresult} for the colorbar -- colorbars are full axis environments, so they have all the options available to "normal" plots. You can set the options for the colorbar using colorbar style={<options>}:

pgfplots with logarithmic colorbar

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

% Define new pgfmath function for the logarithm to base 10 that also works with fpu library
\pgfmathdeclarefunction{lg10}{1}{%
    \pgfmathparse{ln(#1)/ln(10)}%
}

\pgfplotstableread{
createEn depEn tot
0 5 1
0.1 20 10
0.5 100 100
2 10300 1000
}\mytable %


\begin{tikzpicture}
\begin{semilogyaxis}[
    xlabel = create energy (keV),
    ylabel = deposit energy (keV),
    colorbar,
    colorbar style={yticklabel=\pgfmathparse{10^\tick}\pgfmathprintnumber\pgfmathresult}
]

\addplot+[scatter, only marks, scatter src=explicit, mark=square]
 table[meta expr=lg10(\thisrow{tot})] {\mytable};
\end{semilogyaxis}
\end{tikzpicture}


\end{document}
share|improve this answer
    
Perfect! Sorry I didn't simplify my data, like in your example (my file is large). I haven't learned to work with pgfmath yet, so the declare function is very helpful as well. –  ackey Jul 22 '11 at 1:23
    
side note: \pgrpathparse has a range limit, so the above will not work for large (e.g 1e6 dimension too large) or small values (e.g. 1e-6). In this case I found it convenient to use: colorbar style={yticklabel=$10^{\pgfmathparse{\tick}\pgfmathprintnumber\pgfmathresult}$} –  Sebastian Mar 11 at 17:15

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.