2

I create a tikzpicture in standalone mode (see code below). Then import the resultant .pdf file into my document via \includegraphics (see pic). However the axis labels are cut off and part of the ytick when using [trim axis left, trim axis right]. If I don't use that command it looks fine but is not centered any more. Any thoughts?

PS this is a duplicate but perhaps more concise of this Pgfplot figure off-center because of axis label

enter image description here

\documentclass[tikz,border=12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\begin{document}
\definecolor{mycolor1}{rgb}{0.0416666666666667,0,0}

\begin{tikzpicture}[trim axis left, trim axis right]


\begin{axis}[%
width=6.17578125in,
height=2.23276041666667in,
area legend,
scale only axis,
xmin=0.5,
xmax=5.5,
xtick={1,2,3,4,5},
xticklabels={Equipment,Patient,{Hygiene products},Near-bed,Far-bed},
ymin=0,
ymax=0.35,
ytick={  0, 0.1, 0.2, 0.3, 0.4},
ylabel={Probability density},yticklabel style={/pgf/number format/fixed},
legend style={at={(0.143572984749455,0.733206590621038)},anchor=south west,draw=black,fill=white,legend cell align=left}
]
\addplot[ybar,bar width=0.282321428571429in,bar shift=-0.176450892857143in,fill=mycolor1,draw=black] plot coordinates{(1,0.163793103)
(2,0.164367816)
(3,0.141954023)
(4,0.279310345)
(5,0.250574713)};

\addlegendentry{Smith et al.};

\addplot [
color=black,
solid,
forget plot
]
table[row sep=crcr]{
0.5 0\\
5.5 0\\
};
\addplot[ybar,bar width=0.282321428571429in,bar shift=0.176450892857143in,fill=white,draw=black] plot coordinates{(1,0.165289256)
(2,0.185950413)
(3,0.157024793)
(4,0.285123967)
(5,0.20661157)};

\addlegendentry{Adjusted YAB};

\end{axis}
\end{tikzpicture}%
\end{document}

1 Answer 1

2

One thing you could do is (manually) increase the border around the graph. The standalone package allows the left and right borders to be set independently of the top and bottom borders. So, if you change

\documentclass[tikz,border=12pt]{standalone}

to

\documentclass[tikz,border={38pt 12pt}]{standalone}

then your y-axis labels will not be cropped, and your graph will remain centred. The only downside is that this is a bit of a dirty hack!

6
  • Very nice! thank you. By the way how did you decide on 38pt? ie how did you know teh graph would still be centered?
    – HCAI
    May 3, 2013 at 12:09
  • You're welcome. You had already centred the graph using your [trim axis left, trim axis right]. My border={38pt 12pt} then adds a 38pt border to the left and right equally, which keeps the graph centred, whichever numbers are used. I just came up with 38pt by trial and error. May 3, 2013 at 12:13
  • Ah I see! So is the 38pt graph size dependent?
    – HCAI
    May 3, 2013 at 12:32
  • Yes, it depends on the size of the labels and ticks to the left of your axis. In other words, my solution is not particularly elegant. May 3, 2013 at 16:39
  • 1
    Yes. Put xtick pos=left and ytick pos=left in your axis options. (The default is both.) May 4, 2013 at 7:00

You must log in to answer this question.

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