我正在尝试制作磁滞回线样式的图形,并希望跳过图形实际上没有演变的一些 x 值。为此,我尝试将 3 个轴拼接在一起,每个轴都包含整体数据集的一部分。
MWE 如下:
\documentclass[]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\Xa{-7.5}
\newcommand\Xb{-6}
\newcommand\Xc{-1.5}
\newcommand\Xd{1.5}
\newcommand\Xe{6}
\newcommand\Xf{7.5}
\begin{tikzpicture}
\begin{axis}[%
name = mid,
unit vector ratio*=2 1 2,
xmin=\Xc,
xmax=\Xd,
xlabel={X label},
ymin=-4,
ymax=4,
axis background/.style={fill=white},
hide y axis
]
\addplot [color=red,solid,mark=asterisk,mark options={solid},forget plot, restrict x to domain=\Xc:\Xd]
table[]{-1.5 -4
1.5 4
};
\node [inner sep=0,outer sep=0](midleft) at (axis cs:\Xc, -4) {};
\node [inner sep=0,outer sep=0](midright) at (axis cs:\Xd, -4) {};
\end{axis}
\begin{axis}[%
at={(mid.outer west)},anchor=outer east,
unit vector ratio*=2 1 2,
xmin=\Xa,
xmax=\Xb,
xlabel={\vphantom{X label}},
xtick={-7, -6},
ymin=-4,
ymax=4,
ylabel={Y label},
axis background/.style={fill=white},
axis y line*=left
]
\addplot [color=red,solid,mark=asterisk,mark options={solid},forget plot, restrict x to domain=\Xa:\Xb]
table[]{-7 -4
-6 -4
};
\node [inner sep=0,outer sep=0](leftright) at (axis cs:\Xb, -4) {};
\end{axis}
\begin{axis}[%
at={(mid.outer east)},anchor=outer west,
unit vector ratio*=2 1 2,
xmin=\Xe,
xmax=\Xf,
xlabel={\vphantom{X label}},
xtick={6,7},
ymin=-4,
ymax=4,
axis background/.style={fill=white},
axis y line*=right
]
\addplot [color=red,solid,mark=asterisk,mark options={solid},forget plot, restrict x to domain=\Xe:\Xf]
table[]{6 4
7 4
};
\node [inner sep=0,outer sep=0](rightleft) at (axis cs:\Xe, -4) {};
\end{axis}
\draw [blue, thick] (leftright) -- (midleft);
\draw [blue, thick] (midright) -- (rightleft);
\end{tikzpicture}%
\end{document}
如您所见,存在一些问题:首先,x 轴在三个轴之间没有对齐,两个外侧轴似乎对齐了,但与中间轴不对齐。我尝试使用 \vphanton x-label 来纠正这个问题,这有点帮助,但没有让它对齐。
第二,中心与右轴、中心与左轴的距离不相等。
最后,应该“延续” x 轴的蓝线(我计划用摆动“装饰”它们,以便清楚地表明它们不是连续的)没有到达节点位置,有一个微小的间隙,在 PDF 输出中可见。我尝试通过将内部和外部 sep 设置为 0 来纠正,这也有帮助,但并没有完全解决问题。
有人能帮忙解决这些问题吗?
答案1
您犯的主要“错误”是使用“ outer
”锚点来对齐axis
环境。请查看代码中的注释,以详细了解我的解决方案的工作原理。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{
% use `compat' level 1.3 or higher to use the improved positioning
% of the axis labels
compat=1.3,
%
% moved common axis options to this style
my axis style/.style={
unit vector ratio*=2 1 2,
% % commented next line, because I don't know what it is good for
% % in this context
% axis background/.style={fill=white},
},
% moved common `\addplot' option to this style
my addplot style/.style={
color=red,
solid,
mark=asterisk,
mark options={solid},
forget plot,
},
}
\begin{document}
\begin{tikzpicture}
% moved this commands inside the tikzpicture environment so they
% are first localized and second don't produce some whitespaces
\newcommand\Xa{-7.5}
\newcommand\Xb{-6}
\newcommand\Xc{-1.5}
\newcommand\Xd{1.5}
\newcommand\Xe{6}
\newcommand\Xf{7.5}
% define the length which should be gap between the axis environments
\pgfmathsetlengthmacro{\xShift}{5mm}
\begin{axis}[
name=mid,
my axis style,
%
xmin=\Xc,
xmax=\Xd,
xlabel={X label},
ymin=-4,
ymax=4,
hide y axis,
]
\addplot [
my addplot style,
restrict x to domain=\Xc:\Xd,
] table {
-1.5 -4
1.5 4
};
\end{axis}
% create coordinate where "left" axis should end
% (don't use the "outer" anchors, because they depend on the
% axes and tick labels, whereas the "not-outer" anchors don't)
\coordinate (A) at ([xshift=-\xShift] mid.south west);
\begin{axis}[
name=left,
% use the created coordinate to place this axis environment
% and use an appropriate anchor
at={(A)},
anchor=south east,
my axis style,
%
xmin=\Xa,
xmax=\Xb,
xtick={-7, -6},
ymin=-4,
ymax=4,
ylabel={Y label},
axis y line*=left,
]
\addplot [
my addplot style,
restrict x to domain=\Xa:\Xb
] table {
-7 -4
-6 -4
};
\end{axis}
% create coordinate where the "right" axis should begin
\coordinate (B) at ([xshift=\xShift] mid.south east);
\begin{axis}[
name=right,
% same as for the "left" axis
at={(B)},
anchor=south west,
my axis style,
%
xmin=\Xe,
xmax=\Xf,
xtick={6,7},
ymin=-4,
ymax=4,
axis y line*=right,
]
\addplot [
my addplot style,
restrict x to domain=\Xe:\Xf,
] table {
6 4
7 4
};
\end{axis}
% also for the "blue lines" use the
\draw [blue,thick] (mid.south west) -- (left.south east);
\draw [blue,thick] (mid.south east) -- (right.south west);
% this line is just to prove, that the three axis environments
% are really perfectly aligned now
\draw [green,very thin,dashed]
(left.south west) rectangle (right.north east);
\end{tikzpicture}
\end{document}