3

Hope this is not a stupid question. I have the following two plots: Misaligned graphs

As you can see the two y-axes are not aligned properly. I am not doing anything weird with the positioning I believe, as the two y labels are perfectly aligned. I believe the numbers on the y-axis of the top graph are moving the graph a bit on the right, resulting in a misalignment with respect to the bottom graph. Here is how I styled the two graphs:

\begin{figure*}[t]
\begin{tikzpicture}
\begin{axis}[
    legend style={at={(1.05,1)}, anchor=north west,font=\tiny},
    xlabel=Delay (ms),
    ylabel=Number of Messages Executed,
    width=\textwidth/1.18,
    height=\textwidth/2.3,
    axis lines=middle,
    x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
    y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
    axis line style={->},
]
\addplot{
    %plot data
};
%more plots
\legend{C1P1,C1P2,C2P1,C2P2,C3P1,C3P2,C4P1,C4P2}
\end{axis}
\end{tikzpicture}
\\
\begin{tikzpicture}
\begin{axis}[ 
    width=\textwidth/1.18,
    height=\textwidth/2.3,
    boxplot/draw direction=y, 
    x axis line style={opacity=0}, 
    axis x line*=bottom, 
    axis y line=left, 
    enlarge y limits, 
    ymajorgrids, 
    xtick={1, 2, 3, 4, 5, 6, 7, 8}, 
    xticklabels={\footnotesize{C1P1},\footnotesize{C1P2},\footnotesize{C2P1},\footnotesize{C2P2},\footnotesize{C3P1},\footnotesize{C3P2},\footnotesize{C4P1},\footnotesize{C4P2}},
    ylabel={Queue Size}
]
%plots
\end{axis}
\end{tikzpicture} 
\label{blabla}
\caption{blabla}
\vspace{-0.5cm}
\end{figure*}

Thanks!

6
  • You might want to check section 4.19.3 Horizontal Alignment from the pgfplots manual.
    – Mace
    Mar 23, 2017 at 9:49
  • See tex.stackexchange.com/questions/247885/…. Try adding the options [trim axis left,trim axis right], to exclude the labels from width computations.
    – gernot
    Mar 23, 2017 at 9:53
  • Trimming the space doesn't do the work as it moves the top graph all the way to the left, breaking the indentation of the page. While by looking at the pgfplots manual I tried giving a name=delay to the top graph and adding at=(delay.below south west), anchor=north west, to the bottom graph but the result is identical.
    – Masiar
    Mar 23, 2017 at 10:25
  • That's surprising. However, since you didn't provide a working MWE it's not reproducible for me. It seemed to work fine for me with this approach.
    – Mace
    Mar 23, 2017 at 11:16
  • That is not surprising if you are using two separate tikzpicture environments, but if the two axis environments are in the same tikzpicture environment, that should work. Edit: You could also have a look at the groupplots library. Mar 23, 2017 at 11:54

1 Answer 1

1

As stated in the comment above - this works properly:

\documentclass{article}
\usepackage{pgfplots,tikz}
\usepgfplotslibrary{statistics}
\begin{document}
    \begin{figure*}[t]
\begin{tikzpicture}
\begin{axis}[
legend style={at={(1.05,1)}, anchor=north west,font=\tiny},
xlabel=Delay (ms),
ylabel=Number of Messages Executed,
width=\textwidth/1.18,
height=\textwidth/2.3,
axis lines=middle,
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
axis line style={->},
name=main plot
]

\addplot table {
    x y
    1 .1
    2 2
    3 .5
    %plot data
};
%more plots
\legend{C1P1,C1P2,C2P1,C2P2,C3P1,C3P2,C4P1,C4P2}
\end{axis}

\begin{axis}[ at={(main plot.below south west)},yshift=-0.1cm,
anchor=north west,
 width=\textwidth/1.18,
height=\textwidth/2.3,
boxplot/draw direction=y, 
x axis line style={opacity=0}, 
axis x line*=bottom, 
axis y line=left, 
enlarge y limits, 
ymajorgrids, 
xtick={1, 2, 3, 4, 5, 6, 7, 8}, 
xticklabels={\footnotesize{C1P1},\footnotesize{C1P2},\footnotesize{C2P1},\footnotesize{C2P2},\footnotesize{C3P1},\footnotesize{C3P2},\footnotesize{C4P1},\footnotesize{C4P2}},
ylabel={Queue Size}
]
%plots
\addplot table {
    x y
    1 100
    2 200
    3 400
    %plot data
};
\end{axis}
\end{tikzpicture} 
\label{blabla}
\caption{blabla}
\vspace{-0.5cm}
\end{figure*}
\end{document}

If it doesn't for you, please provide a full working MWE. screenshot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .