4

I have a pgfplot that is supposed to give me only a colorbar, no other plot. My code was inspired by this question. The logarithmic colorbar code was recycled from this answer and this answer.

The result I'm getting is off in one respect: I want ticks at all powers of 10, written as such (10^n) instead of ordinary decimals. Also, I don't want the redundant 1* in front of a tick (as is the case now with 10^5).

My output so far (please exue the glitchy rendering):
wrong ticks

And my MWE:

\documentclass{standalone}

\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}

\begin{filecontents}{data.csv}
height,width
1,2
205241,3
205241,0
\end{filecontents}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ymode=log,
    enlargelimits=false,
    hide axis,
    scale only axis,
    height=0,
    width=0,
    colorbar horizontal,
    colorbar style={
        width=10cm,
        xtick={0,1,2,3,4,5},
        xticklabel={
            \pgfkeys{/pgf/fpu}
            \pgfmathparse{10^\tick}\pgfmathprintnumber\pgfmathresult
            \pgfkeys{/pgf/fpu=false}
        },
    },
]
\addplot [
    mesh,
    draw=none,
    point meta=explicit, % use the "meta expr" defined below
] table [
    col sep=comma,
    create on use/xaccum/.style={
        create col/expr=\pgfmathaccuma+\prevrow{width}
    },
    meta expr=lg10(\thisrow{height}), % transform "order" into "linear" for colorbar
    x=xaccum, y=height,
] {data.csv};
\end{axis}
\end{tikzpicture}

\end{document}
4

The \tick macro in the colorbar already contains the exponent, so you can just use xticklabel=$10^{\pgfmathprintnumber{\tick}}$:

\documentclass[border=5mm]{standalone}

\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}

\begin{filecontents}{data.csv}
height,width
1,2
205241,3
205241,0
\end{filecontents}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    enlargelimits=false,
    hide axis,
    scale only axis,
    height=0,
    width=0,
    colorbar horizontal,
    colorbar style={
        width=10cm,
        xtick={0,1,...,6},
        xticklabel=$10^{\pgfmathprintnumber{\tick}}$
    },
]
\addplot [
    mesh,
    draw=none,
    point meta=explicit
] table [
    col sep=comma,
    create on use/xaccum/.style={
        create col/expr=\pgfmathaccuma+\prevrow{width}
    },
    x=xaccum, y=height,
    meta expr=lg10(\thisrow{height})
] {data.csv};
\end{axis}
\end{tikzpicture}

\end{document}

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.