12

I have a colorbar in pgfplots and would like to have the y tick labels aligned to the right.

Using

colorbar style={yticklabel style={align=right}}

did not work for me.

The left colorbar shows the default behaviour and the right the wanted bahaviour but with clumsy code (see MWE below).

colorbar ticklabel aligned and not aligned

MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
colorbar,
point meta min=-20,
point meta max=20,
colorbar style={
    height=10cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}%[xshift=2cm]
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
colorbar,
point meta min=-20,
point meta max=20,
colorbar style={
 yticklabel={$$},
    height=10cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}

\foreach \x [evaluate=\x as \xeval using 20-\x*4] in {0,1.25,...,10}
\node[anchor=east] at (1.75,-\x) {\pgfmathprintnumber{\xeval}};

\end{tikzpicture}
\end{document}![enter image description here][3]
13

I guess the problem is that the nodes containing the ticklabels are only as wide as the width of the individual ticklabel, so even if you align them right, that won't align them in relation to the other ticks.

A workaround would be to set the minimum width of the ticklabel nodes first, then change the alignment:

colorbar style={
    height=10cm,
    yticklabel style={text width=width("$-20$"),align=right}
}

You have to manually insert the widest ticklabel in width("..") though, so it's not entirely automatic.

(Note on width vs. \widthof: pgf/TikZ 2.x implemented a macro \widthof to calculate the width of some text. This was removed in version 3, but instead one can use the width function defined in the pgf math engine as seen above. There is a \widthof macro defined in the calc package though.)

Below I also added draw to the yticklabel style, to illustrate the point.

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
colorbar,
point meta min=-20,
point meta max=20,
colorbar style={
    height=10cm,yticklabel style={draw,align=right}
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
colorbar,
point meta min=-20,
point meta max=20,
colorbar style={
    height=10cm,yticklabel style={draw,text width=width("$-20$"),align=right}
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}%[xshift=2cm]
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
colorbar,
point meta min=-20,
point meta max=20,
colorbar style={
 yticklabel={$$},
    height=10cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}

\foreach \x [evaluate=\x as \xeval using 20-\x*4] in {0,1.25,...,10}
\node[anchor=east] at (1.75,-\x) {\pgfmathprintnumber{\xeval}};

\end{tikzpicture}
\end{document}
|improve this answer|||||
  • Hej Torbjørn - long time no see... Your works fine, thanks. I was hoping there would be an 'automatic' solution, but I can live with yours. – mathause Nov 15 '13 at 12:29
  • @Thethos Hello there, didn't recognize your profile image at first. Glad I could help. – Torbjørn T. Nov 15 '13 at 12:34

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.