![带有 2 个 x 轴的 pgfplots,上部 x xticklabels 未对齐](https://linux22.com/image/257590/%E5%B8%A6%E6%9C%89%202%20%E4%B8%AA%20x%20%E8%BD%B4%E7%9A%84%20pgfplots%EF%BC%8C%E4%B8%8A%E9%83%A8%20x%20xticklabels%20%E6%9C%AA%E5%AF%B9%E9%BD%90.png)
我需要一个有两个 x 轴的图,一个在顶部,另一个在底部。我尝试使用以下示例代码成功完成此操作:
\documentclass[12pt,twoside, a4paper]{report}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}
%\pgfplotsset{set layers}%Not used
\begin{semilogyaxis}[filter discard warning=false,scale only axis,
width=5.41cm,
height=4.415cm,
xmin=-7.5,xmax=7.5,ymin=0.01,ymax=50,
axis x line*=bottom,
xtick={-6,-4,-2,0,2,4,6},
enlargelimits=false,axis on top=true,
xlabel=lower x scale,ylabel=y scale]
\addplot[color=red,mark=x] coordinates {%just fake data
(-6,0.1) (0,10) (6,0.2)};
\end{semilogyaxis}
\begin{semilogyaxis}[filter discard warning=false,
scale only axis,
width=5.41cm,
height=4.415cm,
xmin=-0.7409,xmax=0.7409,ymin=0.01,ymax=50,
minor tick num=3,
axis x line*=top,
axis y line=none,
enlargelimits=false,axis on top=true,
xlabel=upper x scale]
\end{semilogyaxis}
\end{tikzpicture}
\end{document}
结果非常好,正如您所看到的:
但由于左上角的“-0.5”,标签略有错位。我该如何解决这个问题?
答案1
减号的文本深度比标签中的其他字符更大,导致错位。您可以设置text depth=0pt
以解决此问题:
\documentclass[12pt,twoside, a4paper]{report}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}
%\pgfplotsset{set layers}%Not used
\begin{semilogyaxis}[filter discard warning=false,scale only axis,
width=5.41cm,
height=4.415cm,
xmin=-7.5,xmax=7.5,ymin=0.01,ymax=50,
axis x line*=bottom,
xtick={-6,-4,-2,0,2,4,6},
enlargelimits=false,axis on top=true,
xlabel=lower x scale,ylabel=y scale]
\addplot[color=red,mark=x] coordinates {%just fake data
(-6,0.1) (0,10) (6,0.2)};
\end{semilogyaxis}
\begin{semilogyaxis}[filter discard warning=false,
scale only axis,
width=5.41cm,
height=4.415cm,
xmin=-0.7409,xmax=0.7409,ymin=0.01,ymax=50,
xticklabel style={text depth=0pt},
minor tick num=3,
axis x line*=top,
axis y line=none,
enlargelimits=false,axis on top=true,
xlabel=upper x scale]
\end{semilogyaxis}
\end{tikzpicture}
\end{document}