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 am aligning many pgfplots with the TikZ \matrix. The axes are automatically aligned correctly. How can I also align the ylabels automatically (because I am changing plotted data very often)?

\usepackage[latin1]{inputenc}
\usepackage[USenglish]{babel}

\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure} [H]
    \centering
    \begin{tikzpicture}
    \matrix{
       \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1}]
           \addplot {x^2};
        \end{axis}
        \\
        \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2}]
            \addplot {-(x^2)};
        \end{axis}
        \\  
     };
\end{tikzpicture}
\end{figure}

\end{document}

Not desired output:

not desired

Desired output:

desired

Again I had a close look at:

  • Manual for pgfplots by Christian Feuersaenger
  • Manual for TikZ & PGF by Till Tantau

edit

thanks! Especially the the finer control is really good.

Unfortunately now -- with this ylabel absolute -- linebreaks with \\ or \newline aren't possible anymore :-(

Is there a trick?

not working code:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
compat=newest,
 /pgfplots/myylabel absolute/.style={%
  /pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90},
  /pgfplots/every y tick scale label/.style={
   at={(0,1)},above right,inner sep=0pt,yshift=0.3em
   }
   }
 }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
   \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel  1 first line\\second line},myylabel absolute=-60]
       \addplot {x^2};
    \end{axis}
    \\
    \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2 first line\\second line},,myylabel absolute=-60]
        \addplot {-(x^2)};
    \end{axis}
    \\  
 };
\end{tikzpicture}
\end{figure}

\end{document} 
share|improve this question
    
I added a new option to my updated answer. –  Gonzalo Medina Jul 8 '13 at 2:37
    
I updated my answer with a solution for the new requirement. –  Gonzalo Medina Jul 8 '13 at 15:10

1 Answer 1

up vote 12 down vote accepted

The ylabel absolute option (pages 173 and 174 of the pgfplots manual) produces the desired result:

\documentclass{article}

\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
    \matrix{
       \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},ylabel absolute]
           \addplot {x^2};
        \end{axis}
        \\
        \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},ylabel absolute]
            \addplot {-(x^2)};
        \end{axis}
        \\  
     };
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

If you want even finer control, you can define a style in a similar fashion to ylabel absolute, but allowing you to specify, through an argument, the desired shifting (ylabel absolute uses -35pt):

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
compat=newest,
/pgfplots/myylabel absolute/.style={%
  /pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90},
  /pgfplots/every y tick scale label/.style={
    at={(0,1)},above right,inner sep=0pt,yshift=0.3em
   }
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
    \matrix{
       \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},myylabel absolute=-30pt]
           \addplot {x^2};
        \end{axis}
        \\
        \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},,myylabel absolute=-30pt]
            \addplot {-(x^2)};
        \end{axis}
        \\  
     };
\end{tikzpicture}

    \begin{tikzpicture}
    \matrix{
       \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},myylabel absolute=-40pt]
           \addplot {x^2};
        \end{axis}
        \\
        \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},,myylabel absolute=-40pt]
            \addplot {-(x^2)};
        \end{axis}
        \\  
     };
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

After the edit to the original question: the problem is that you are trying to use labels with more than one line of text; in order to do so, you can, for example, use the align=<value> option for the label:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
compat=newest,
 /pgfplots/myylabel absolute/.style={%
  /pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90,align=center},
  /pgfplots/every y tick scale label/.style={
   at={(0,1)},above right,inner sep=0pt,yshift=0.3em
   }
 }
}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
   \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel  1 first line\\second line},myylabel absolute=-50]
       \addplot {x^2};
    \end{axis}
    \\
    \begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2 first line\\second line},myylabel absolute=-50]
        \addplot {-(x^2)};
    \end{axis}
    \\  
 };
\end{tikzpicture}
\end{figure}

\end{document} 

enter image description here

share|improve this answer
    
thanks a lot! works great and I am 99 % satisfied :-) The last 1 % would be to get the modified picture really centered over the caption :-) –  Benjamin Massow Jul 9 '13 at 20:58
1  
@BenjaminMassow by default, the whole picture (including the labels) is centered with respect to the caption; if you want to exclude the labels, you can use trim axis left for tikzpicture, as in \begin{tikzpicture}[trim axis left]...\end{tikzpicture}` –  Gonzalo Medina Jul 9 '13 at 23:18
    
:-) that's it, thx! –  Benjamin Massow Jul 17 '13 at 10:20

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.