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.

Can somebody help me? I try to align two plots horizontally in pgfplots! In the preamble I have:

MWE*


\documentclass[12pt]{article}
\usepackage{pgfplots,subcaption}
\pgfplotsset{ compat=1.7,
dacrip plot/.style={ % Set common styles that can be applied to the axis environment
    x=2cm, y=2cm,                   % Unit vector length
    axis lines=middle,              % Axis lines go through (0,0)
    xtick=\empty, ytick=\empty,     % No tick marks
    ymin=0, ymax=1.75,              % Y axis range
    enlargelimits=upper,            % Increase upper range a bit
    clip=false,                     % Allow labels to extend beyond the axis
    disabledatascaling,             % Allow to draw TikZ objects without having to use (axis cs:<x>,<y>)
    x label style={anchor=north},   % Alignment of axis labels
    y label style={anchor=east},
    yticklabel style={align=right}
}
}    
\begin{document}

\begin{figure}[!h]\centering
\begin{subfigure}[t]{0.48\textwidth} \centering
\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel=$f$, ylabel=$U$]
       \addplot [blue, ultra thick] coordinates {(0,0) (1,1) (2,1) };
        \draw [dashed] (0,1) -- (1,1) node [pos=0, anchor=east] {$U_{max}$};
   \end{axis}
\end{tikzpicture}
\subcaption{Plot 1}\label{fig:ufMna}
\end{subfigure}
\begin{subfigure}[t]{0.48\textwidth} \centering

\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel=$n$, ylabel=$M$ ]
       \addplot [blue, ultra thick] coordinates {(0,1) (1,1)} parabola [bend at end] (2,0.3);
   \end{axis};

\end{tikzpicture}
\subcaption{Plot 2}\label{fig:ufMnb}
\end{subfigure}
\caption{2 Plots}
\label{fig:ufMn}
\end{figure}
\end{document}

enter image description here

The problem is that the x-axis of Plot 2 is to short. Another problem is that the alignment of the x-axis is there, but the captions of the two plots aren't aligned!


* Modified from OP's snippet

share|improve this question
    
You might find useful the groupplots library: see as reference How to align PGFplot subplots and Using pgfplots, add vertical/horizontal spacing between plots? –  Claudio Fiandrino Feb 22 '13 at 8:10
    
Could you turn the code snippet into a complete compilable document (starting from \documentclass)? –  Jake Feb 22 '13 at 8:19

1 Answer 1

I cannot compile all your stuff, because of missing headers, but this should align the caption:

Adding a \vphantom{f] in your xlabel will add the missing vertical space to align the subcaptions:

\documentclass[12pt]{article}
\usepackage{pgfplots,subcaption}
\pgfplotsset{ compat=1.7,
dacrip plot/.style={ % Set common styles that can be applied to the axis environment
    x=2cm, y=2cm,                   % Unit vector length
    axis lines=middle,              % Axis lines go through (0,0)
    xtick=\empty, ytick=\empty,     % No tick marks
    ymin=0, ymax=1.75,              % Y axis range
    enlargelimits=upper,            % Increase upper range a bit
    clip=false,                     % Allow labels to extend beyond the axis
    disabledatascaling,             % Allow to draw TikZ objects without having to use (axis cs:<x>,<y>)
    x label style={anchor=north},   % Alignment of axis labels
    y label style={anchor=east},
    yticklabel style={align=right}
}
}    
\begin{document}

\begin{figure}[!h]\centering
\begin{subfigure}[t]{0.48\textwidth} \centering
\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel=$f$, ylabel=$U$]
       \addplot [blue, ultra thick] coordinates {(0,0) (1,1) (2,1) };
        \draw [dashed] (0,1) -- (1,1) node [pos=0, anchor=east] {$U_{max}$};
   \end{axis}
\end{tikzpicture}
\subcaption{Plot 1}\label{fig:ufMna}
\end{subfigure}
\begin{subfigure}[t]{0.48\textwidth} \centering

\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel={$n$\vphantom{f}}, ylabel=$M$ , xmax=2]
       \addplot [blue, ultra thick] coordinates {(0,1) (1,1)} parabola [bend at end] (2,0.3);
   \end{axis};

\end{tikzpicture}
\subcaption{Plot 2}\label{fig:ufMnb}
\end{subfigure}
\caption{2 Plots}
\label{fig:ufMn}
\end{figure}
\end{document}

I think that TeX will use the bounding box to position picture and caption, but in your case, your 2 pictures do not have the same size (here depth). Adding a \vphantom in the xlabel should add the required space to align the bottom of each graph.

Edit: for the second question, you could manually adjust the x-axis via the xmax key.

output

share|improve this answer
    
Genius! Your \vphantom trick works, but you have forgotten the $$ signs around the f! –  dacrip Feb 22 '13 at 9:20
    
Are there any suggestions for the problem with the x-axis in plot 2 ? –  dacrip Feb 22 '13 at 9:20
    
You don't need $$ around the f, as all letter with "depth" have the same depth; using g or p instead f work also well –  Lionel MANSUY Feb 22 '13 at 9:26
    
I have updated my answer for your second question –  Lionel MANSUY Feb 22 '13 at 10:02
    
$U_{max}$ should be $U_{\max}$ –  pluton Feb 22 '13 at 13:09

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.