# How do I align a pgfplots graph?

I'm trying to draw a graph with the associated table of data using pgfplots. It works great, but the graph itself isn't aligned with the table. This is what it looks like:

I don't like the overhang. How can I move the position of the graph?

I would like to either line up one side or another. This is what it would look like if I could line them up to the right:

I could deal with only being able to do that, but it would also be useful to have a more general way to move the graph.

I've looked around in the pgfplots manual, as well as the pgf manual, but I couldn't find anything that looked promising. I'm fairly new to LaTeX, only been using it for a few days, so feel free to just point me in the right direction.

Code:

Results:\    \begin{tabular}{| c | c | c |}
\hline
\textbf{QP} & \textbf{Average PSNR\_Y ($dB$)} & \textbf{Total bits} \        \hline
1 & 49.197166 & 14593504\        \hline
8 & 32.003399 & 1106824\        \hline
16 & 28.387211 & 374880\        \hline
24 & 26.661640 & 206704\        \hline
31 & 25.740038 & 150056\        \hline
\end{tabular}

\vspace*{2em}

\noindent
\begin{tikzpicture}
\begin{axis}[
xlabel=$QP$,
ylabel={$Encoding Quality$}
]
% use TeX as calculator:
%   \addplot {x^2 - x +4};
(1,49.197166)
(8,32.003399)
(16,28.387211)
(24,26.661640)
(31,25.740038)
};
(1,58.374016)
(8,4.427296)
(16,1.49952)
(24,0.826816)
(31,0.600224)

};
\addlegendentry{Total Bits $\times250,000$}

\end{axis}
\end{tikzpicture}

-

I think the trim right and trim left options are only useful if there is something which sticks out of the axis' box -- for example, if you want to exclude a legend from the horizontal alignment.

In your case, it appears to me that it suffices to surround the table and the graphics with

\begin{flushright}
...
\end{flushright}


If you have multiple elements which you want to align, you can also pack all of them into a tabular (which supports left-aligned or right-aligned columns in a natural way).

Once you start with horizontal and vertical alignment, you should consider using the \begin{tikzpicture}[baseline] option and the \begin{tabular}[c] flag as well -- both are part of the associated control mechanisms (which are explained in more detail in the pgfplots manual, there is a section "Alignment" somewhere which should also cover these specific cases).

-

How do you insert the graph and the table?If you wrote

\begin{table}...\end{table}\\
\begin{tikzpicture}...\end{tikzpicture}


try with

\begin{table}...\end{table}\\
\noindent \begin{tikzpicture}...\end{tikzpicture}

-
This worked for my my first graph - For this particular table and graph it looks good, but I've got another one that it doesn't work so well for. –  Wayne Werner Apr 27 '11 at 14:25
If the code is similar it should work the same way. If not, please show the differences –  Spike Apr 27 '11 at 17:40

You can use absolute positioning with

\begin{tikzpicture}[remember picture, overlay]
\begin{axis}[
xlabel=$QP$,
ylabel={$Encoding Quality$},
at=(current page.west),
shift={(10.8em,-13em)},
]
...
\end{tikzpicture}


The exam sheet example here shows a little more. Page 198 of the pgf manual has some more information about remember picture, overlay. Using these commands along with \vspace*{} will give you as much control as you want. You can use px or any other standard units in the shift coordinates.

-

You could use the trim left and trim right options.

Eg. you should write:

\begin{tikzpicture}[trim left=0cm]
...plot code...
\end{tikzpicture}


See also the pgf manual, page 167 for a description on how to use it correctly. Basically what it does is ignore everything that is left of the given trim left x-coordinate and right of the trim right coordinate.

So in the above example, everything to the left of x=0 is ignored for the bounding box.

Play around with it till you get the results you want.

-
When I add trim and try to compile I get an emergency stop. I tried copy/pasting the example from the pgf manual, and got an error too. –  Wayne Werner Apr 27 '11 at 14:41
what's the error? –  romeovs Apr 27 '11 at 14:48
That's all the info it gives me - Emergency stop, and exit with errorcode 1 –  Wayne Werner Apr 27 '11 at 18:04
don't know what that means sorry, try the answer below or submit a MWE so I can help you furthur. –  romeovs Apr 27 '11 at 19:03