我想问一下是否有办法对齐负 xtick 标签(带有“减号”的标签),以便它们像正 xtick 标签一样对齐(图片上的绿色框)。我的意思是数字直接对齐在刻度线下方,对齐时忽略“减号”。图片上的负数对齐(红色框)看起来很糟糕。
最小 WME:
\documentclass{article}%
\usepackage{pgfplots}%
\pgfplotsset{compat=newest}%
\begin{document}%
\begin{tikzpicture}%
\begin{semilogyaxis}[
log plot exponent style/.style={
/pgf/number format/fixed zerofill,
/pgf/number format/precision=1},
domain=-5:10]%
\addplot {exp(x)};%
\addplot {exp(2*x)};%
\legend{$e^x$,$e^{2x}$}%
\end{semilogyaxis}%
\end{tikzpicture}%
\end{document}%
答案1
这是使用的可能性xticklabel
。我在代码中添加了一些注释。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
log plot exponent style/.style={
/pgf/number format/fixed zerofill,
/pgf/number format/precision=1},
domain=-5:10,
width=\linewidth,
xticklabel={
% test if the x value is below zero
\ifdim \tick pt < 0pt
% if yes, calculate the absolute value
\pgfmathparse{abs(\tick)}%
% and print first a minus sign in a zero-width box, followed by the absolute value
\llap{$-{}$}\pgfmathprintnumber{\pgfmathresult}
\else
% if no, print as usual
\pgfmathprintnumber{\tick}
\fi
}
]%
\addplot {exp(x)};
\addplot {exp(2*x)};
\legend{$e^x$,$e^{2x}$}
\end{semilogyaxis}
\end{tikzpicture}
\end{document}