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 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]
share|improve this question

1 Answer 1

up vote 10 down vote accepted

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=\widthof{$-20$},align=right}
}

You have to manually insert the widest ticklabel in \widthof though, so it's not entirely automatic.

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

enter image description here

\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,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=\widthof{$-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}
share|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. –  Thethos 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

 
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.