3

I'm trying to do a hysteresis loop style figure, and would like to skip some x-values where the figure doesn't really evolve. To that end, I try to splice together 3 axis, each containing a part of the overall data set.

MWE below:

\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}

The result is as follows Result of above code

As you can see, there are some issues: first, the x-axis does not align between the three axis, the two outer seems to be aligned, but not with the center one. I have tried to correct this with a \vphanton x-label, that helped a little, but did not get it in line.

Second, the distance between the center and right, and center and left, axis is not equal.

Finally, the blue lines that should "continue" the x-axis (I plan to "decorate" these with wiggles such that it is clear that they are not continuous) does not reach the node position, there is a slight gap, visible in the PDF output. I tried to correct by setting inner and outer sep to 0, which also helped, but didn't fix it completely.

Can anyone help fix these issues?

3

The main "mistake" you are doing is to use the "outer" anchors to align the axis environments. Please have a look at the comments in the code for more details how my solution works.

% 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}

image showing the result of above code

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.