2

I try to draw a heatmap using pgfplots, but I can not get correct tick values in a colorbar accompanying the heatmap if z is logarithmic. What is the source of the values around 10^-11? There is nothing similar in the data file.

The file

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{map.txt}
    0.1 0   2.933100901655603E-6    
    0.1 10  1.2378929491530841E-5   
    0.1 20  2.0588051604902755E-5   
    0.1 30  2.7190941686966532E-5   
    0.1 40  3.290721897852047E-5    
    0.1 60  4.250275971734089E-5    
    0.1 80  5.0350197981567186E-5   
    0.1 100 5.681643648591518E-5    
    0.1 150 6.859558257512272E-5    
    0.1 200 7.652271637806553E-5    
    0.1 250 8.22752949739641E-5 
    0.1 300 8.668070617985642E-5    

    0.2 0   1.362157048411715E-5    
    0.2 10  1.4186557576426701E-5   
    0.2 20  1.9510106350903664E-5   
    0.2 30  2.3880991840192088E-5   
    0.2 40  2.7472817836855655E-5   
    0.2 60  3.282892845070262E-5    
    0.2 80  3.656164586607819E-5    
    0.2 100 3.944614546159667E-5    
    0.2 150 4.434691145348564E-5    
    0.2 200 4.730750444203243E-5    
    0.2 250 4.9180711846180925E-5   
    0.2 300 5.0512424521156164E-5   
\end{filecontents}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    colormap/jet,
    colorbar,
    colorbar style={yticklabel=$10^{\pgfmathprintnumber{\tick}}$},
    view={0}{90},
    zmode=log]
    \addplot3[surf,shader=interp] table[x index=0,y index=1,z index=2] {map.txt};
\end{axis}
\end{tikzpicture}
\end{document} 

produces the following image:

heatmap

  • please don't use jet. Consider anything else. – percusse Jan 11 '18 at 2:16
  • but what is jet? – arataj Jan 11 '18 at 9:12
  • @percusse ok it is a color scheme. I just used the default and wanted to change it because of the somewhat flat blue part. – arataj Jan 11 '18 at 9:33
5

New Answer

A simple solution to what you want is to not use a logarithmic z-axis directly, but to calculate that values using z expr=log10(\thisrowno{2}).

% used PGFPlots v1.15
\begin{filecontents}{map.txt}
    0.1 0   2.933100901655603E-6
    0.1 10  1.2378929491530841E-5
    0.1 20  2.0588051604902755E-5
    0.1 30  2.7190941686966532E-5
    0.1 40  3.290721897852047E-5
    0.1 60  4.250275971734089E-5
    0.1 80  5.0350197981567186E-5
    0.1 100 5.681643648591518E-5
    0.1 150 6.859558257512272E-5
    0.1 200 7.652271637806553E-5
    0.1 250 8.22752949739641E-5
    0.1 300 8.668070617985642E-5

    0.2 0   1.362157048411715E-5
    0.2 10  1.4186557576426701E-5
    0.2 20  1.9510106350903664E-5
    0.2 30  2.3880991840192088E-5
    0.2 40  2.7472817836855655E-5
    0.2 60  3.282892845070262E-5
    0.2 80  3.656164586607819E-5
    0.2 100 3.944614546159667E-5
    0.2 150 4.434691145348564E-5
    0.2 200 4.730750444203243E-5
    0.2 250 4.9180711846180925E-5
    0.2 300 5.0512424521156164E-5
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        view={0}{90},
        colormap/jet,
        colorbar,
        colorbar style={yticklabel=$10^{\pgfmathprintnumber{\tick}}$},
    ]
        \addplot3 [
            surf,
            shader=interp,
        ] table [
            x index=0,
            y index=1,
            z expr=log10(\thisrowno{2}),    % <-- this does what you want
        ] {map.txt};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Old Answer (to Old Question)

PGFPlots is exactly doing what you are telling it to do. Your data file contains values of the range 2.9e-6 to 8.7e-5. If you write yticklabel=$10^{\pgfmathprintnumber{\tick}}$ you get exactly that. so instead of just the values 2,4,...,8 and the multiplier 10^-5 on top of the colorbar you get 10^2,10^4,...,10^8. If you in addition have zmode=log you of course get as \tick ln(value) which gives for values around 10^-5 -11, but because of yticklabel=$10^{\pgfmathprintnumber{\tick}}$ it is written 10^-11 ...

So simply comment the two lines causing the trouble and everything should be fine, right?

% used PGFPlots v1.15
\begin{filecontents}{map.txt}
    0.1 0   2.933100901655603E-6
    0.1 10  1.2378929491530841E-5
    0.1 20  2.0588051604902755E-5
    0.1 30  2.7190941686966532E-5
    0.1 40  3.290721897852047E-5
    0.1 60  4.250275971734089E-5
    0.1 80  5.0350197981567186E-5
    0.1 100 5.681643648591518E-5
    0.1 150 6.859558257512272E-5
    0.1 200 7.652271637806553E-5
    0.1 250 8.22752949739641E-5
    0.1 300 8.668070617985642E-5

    0.2 0   1.362157048411715E-5
    0.2 10  1.4186557576426701E-5
    0.2 20  1.9510106350903664E-5
    0.2 30  2.3880991840192088E-5
    0.2 40  2.7472817836855655E-5
    0.2 60  3.282892845070262E-5
    0.2 80  3.656164586607819E-5
    0.2 100 3.944614546159667E-5
    0.2 150 4.434691145348564E-5
    0.2 200 4.730750444203243E-5
    0.2 250 4.9180711846180925E-5
    0.2 300 5.0512424521156164E-5
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        view={0}{90},
        colormap/jet,
        colorbar,
%        colorbar style={yticklabel=$10^{\pgfmathprintnumber{\tick}}$},
%        zmode=log,
    ]
        \addplot3[surf,shader=interp] table[x index=0,y index=1,z index=2] {map.txt};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

| improve this answer | |
  • Thanks, but why "of course it uses ln(x)"? It is the first drawing package I know which defaults do a natural logarithm, and it seems charts which use natural logarithms are rarer. – arataj Jan 11 '18 at 9:19
  • The PGFPlots v1.15 manual states in section 4.2 on page 41: "The logarithmic scaling means to apply the natural logarithm (base e) to each [...] coordinate. Furthermore, ticks will be typeset as 10^<exponent>, [...]" ... – Stefan Pinnow Jan 11 '18 at 9:47
  • Ok but I want the logarithmic scale, and without that line "causing trouble" I get ticks like -11. Now I guess I should go search whether there is some switch to make pgfplots use the common logarithm or put somewhere into pgfplots' macros a conversion to common logarithms or lookup tex' calculating capabilities , because your answer does not quite answer this. – arataj Jan 11 '18 at 9:50
  • Correct, this isn't answered (yet), because up to now you never asked for that :) Do you want to ask a follow-up question or do you want to edit this question? – Stefan Pinnow Jan 11 '18 at 10:04
  • I edited the question, I hope that it is now clearer and more focused. – arataj Jan 11 '18 at 10:16

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.