units
我想知道是否有人在使用 中的库时遇到了和我相同的结果。当使用pgfplots
选项更改时,似乎在 x 轴下放置了一个“额外”的空格。这是一个 MWE:x base=true, x SI prefix=milli
pgfplots
\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}
其结果如下:
相反,当 x 轴在 '0' 到 '10' 范围内时,不使用 'units' 库
\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}
输出以下内容:
请注意,在最后一张图片中,x 轴下方的空白较少。有人知道是什么原因造成的吗?我该如何消除这些多余的空白?如果您能提供意见,我将不胜感激。
答案1
在第一种情况下,如果没有库进行缩放units
,刻度将被缩放,并且 x 轴下方会有一个乘数,即$\cdot 10^{-3}$
此屏幕截图中所示。
通过units
库进行缩放后,该节点为空,但它仍然存在,因此会影响边界框。添加every x tick scale label/.append style={draw,minimum size=1cm}
以查看它是否存在。
因此,您需要移动该节点,例如通过添加at={(rel axis cs:0.5,0.5)}
到样式。或者您可以添加overlay
到样式,这意味着 TikZ 在计算边界框时会忽略它。
\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}