2

I was wondering if anyone has had the same results which I'm having when using the library units from pgfplots. When the options change x base=true, x SI prefix=milli are used, pgfplots seems to put an 'extra' whitespace under the x axis. Here is a MWE:

\documentclass[class=IEEEtran]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary[backgrounds]
\definecolor{graphicbackground}{rgb}{0.96,0.96,0.8}

\begin{document}
\begin{tikzpicture}[tight background ,show background rectangle,
 background rectangle/.style={fill=graphicbackground}]%
\begin{axis}[%
scale only axis,
xlabel={$t$ (ms)},
ylabel={$f(t) = t^2 - t +4$},
xmin=1.8E-3, xmax=8.5E-3,
ymin=-8.5, ymax= -2.5,
axis x line=bottom,
axis y line=left,
axis line style = {-latex},
change x base=true, x SI prefix=milli,
every axis x label/.style={at={(ticklabel* cs:1.0)},anchor=south east},
]%
\addplot[color=red,mark=x] coordinates {
    (2E-3,-2.8559703)
    (3E-3,-3.5301677)
    (4E-3,-4.3050655)
    (5E-3,-5.1413136)
    (6E-3,-6.0322865)
    (7E-3,-6.9675052)
    (8E-3,-7.9377747)
};
\end{axis}%
\end{tikzpicture}
\end{document}

Which produces the following:

extra white space

As opposed when the x-axis is in the range from '0' to '10', in which case the 'units' library is not used

\documentclass[class=IEEEtran]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary[backgrounds]
\definecolor{graphicbackground}{rgb}{0.96,0.96,0.8}

\begin{document}
\begin{tikzpicture}[tight background ,show background rectangle,
 background rectangle/.style={fill=graphicbackground}]%
\begin{axis}[%
scale only axis,
xlabel={$t$ (ms)},
ylabel={$f(t) = t^2 - t +4$},
%xmin=1.8E-3, xmax=8.5E-3,
xmin=1.8, xmax=8.5, % <- new x values
ymin=-8.5, ymax= -2.5,
axis x line=bottom,
axis y line=left,
axis line style = {-latex},
%change x base=true, x SI prefix=milli,
every axis x label/.style={at={(ticklabel* cs:1.0)},anchor=south east},
]%
\addplot[color=red,mark=x] coordinates {
    (2,-2.8559703) %<- x values changed
    (3,-3.5301677)
    (4,-4.3050655)
    (5,-5.1413136)
    (6,-6.0322865)
    (7,-6.9675052)
    (8,-7.9377747)
};
\end{axis}%
\end{tikzpicture}
\end{document}

which outputs the following:

no extra whitespace

Notice in the last image there is less whitespace under the x-axis. Does anyone knows what might be causing this? and how can I get rid of this extra whitespace? I would appreciate your comments.

1

In your first case, without the scaling done by the units library, the ticks would be scaled, and there would be a multiplier present below the x-axis, i.e. the $\cdot 10^{-3}$ in this screenshot.

enter image description here

With the scaling done by the units library, that node is empty, but it's still there, and thus it affects the bounding box. Add every x tick scale label/.append style={draw,minimum size=1cm} to see that it's there.

Hence, you need to move that node, e.g. by adding at={(rel axis cs:0.5,0.5)} to the style. Or you can add overlay to the style, which means TikZ ignores it when calculating the bounding box.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary[backgrounds]
\definecolor{graphicbackground}{rgb}{0.96,0.96,0.8}

\begin{document}
\begin{tikzpicture}[tight background ,show background rectangle,
 background rectangle/.style={fill=graphicbackground}]%
\begin{axis}[%
scale only axis,
xlabel={$t$ (ms)},
ylabel={$f(t) = t^2 - t +4$},
xmin=1.8E-3, xmax=8.5E-3,
ymin=-8.5, ymax= -2.5,
axis x line=bottom,
axis y line=left,
axis line style = {-latex},
change x base=true, x SI prefix=milli,
every axis x label/.style={at={(ticklabel* cs:1.0)},anchor=south east},
every x tick scale label/.append style={at={(rel axis cs:0.5,0.5)}} %%% <-- 
]%
\addplot[color=red,mark=x] coordinates {
    (2E-3,-2.8559703)
    (3E-3,-3.5301677)
    (4E-3,-4.3050655)
    (5E-3,-5.1413136)
    (6E-3,-6.0322865)
    (7E-3,-6.9675052)
    (8E-3,-7.9377747)
};
\end{axis}%
\end{tikzpicture}
\end{document}
  • Thanks Torbjorn, that seems to do the trick! :) I thought there was a 'node' down there somewhere, but I couldn't find it. Thanks once again! – SebastianRV Mar 1 '17 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.