2

Despite multiple ordinates and axes discontinuities appearing adjacent in the pgfplots manual, achieving both in the same plot is proving difficult.

Specifically, I have the following pgfplots plot with two y-axes (see MWE below)

Plot_with_2_y_axes

I would like to have an x-axis discontinuity that breaks the x-axis into 2 intervals: the first ranging from (-4,-3) and the second ranging from (0,4). I would like to retain the border to the graph in the style of the answer to this post, which has a discontinuity on the y-axis. I have tries updating it with no success and i'm not sure how to adapt the answer to a plot with double y-axes.

Any help is appreciated.

MWE

\documentclass[tikz]{standalone}
\usepackage{pgf,pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{tikzpicture} \pgfplotsset{set layers} \begin{axis}[scale only axis, xlabel={$x$}, ylabel={Ordinate 1}, axis y line*=left,
xmin=-4.0, xmax=4.0 ]
\addplot[red] {x^2}; \label{plot_one} \end{axis}

\begin{axis}[scale only axis, ylabel={Ordinate 2}, xmin=-4.0, xmax=4.0, axis y line*=right, axis x line=none,
]
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1} \addplot[blue] {x}; \label{plot_two} \addplot[green] {3*x}; \label{plot_three} \addlegendentry{plot 2} \addlegendentry{plot 3} \end{axis}

\end{tikzpicture} \end{document}

Stefan Pinnow
  • 29,535
Sid
  • 1,806

2 Answers2

3
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document} 
\begin{tikzpicture}
\pgfplotsset{set layers}
\begin{axis}[
title={A confusing and unnecessary plot},
scale only axis,
xlabel={$x$},
ylabel={Ordinate 1},
axis y line*=left,      
xmin=-4, xmax=2,
xticklabels={-4,-4,-3,0,1,2,3,4},
x axis line style= { draw=none}
]                   
\addplot[
  red, samples=101,
  unbounded coords=jump,
  x filter/.expression={x<-3? x: (x>0? x-2: nan)},
  ] {x^2}; \label{plot_one}
  \path[-] (rel axis cs:0,0)     coordinate(botstart)
          --(rel axis cs:0.2,0)coordinate(interruptbotA)
         (rel axis cs:0.3,0)  coordinate(interruptbotB)
         --(rel axis cs:1,0)   coordinate(botstop);
         \path[-] (rel axis cs:0,1)     coordinate(topstart)
         --(rel axis cs:0.7,1) coordinate(interrupttopA)
         (rel axis cs:0.87,1)  coordinate(interrupttopB)
         --(rel axis cs:1,1)   coordinate(topstop);
\end{axis}
\draw(botstart)-- (interruptbotA) decorate[decoration=zigzag]{--(interruptbotB)} -- (botstop);
\draw(topstart) -- (topstop);
\begin{axis}[scale only axis,
ylabel={Ordinate 2},
xmin=-4.0, xmax=2, 
axis y line*=right,
axis x line=none,   
]                       
\addlegendimage{/pgfplots/refstyle=plot_one}
\addlegendentry{plot 1}
\addplot[blue, samples=101,
  unbounded coords=jump,
  x filter/.expression={x<-3? x: (x>0? x-2: nan)},
  ] {x}; \label{plot_two}
\addplot[green, samples=101,
  unbounded coords=jump,
  x filter/.expression={x<-3? x: (x>0? x-2: nan)},
  ] {3*x};  \label{plot_three}
\addlegendentry{plot 2}
\addlegendentry{plot 3} 
\end{axis}
\end{tikzpicture}
\end{document}

Broken plot

Discontinuous axis taken from here: https://tex.stackexchange.com/a/386663/8650

1

This is as far as I got. I tried using axis x discontinuity but there is no way to place it into the right position. It wants to go to the right of 0, or to the left of xmax if negative.

I also ran into some bugs regarding the axis environment and shifting. If you put the axis into a scope and shift the scope, the only thing moved is the legend. If you shift the axis, the bounding box is not shifted.

\documentclass{standalone}
\usepackage{pgf,pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{tikzpicture} \pgfplotsset{set layers}% ??? \begin{axis}[scale only axis, width=1cm, height=4cm, xlabel=\vphantom{$x$}, ylabel={Ordinate 1}, axis y line*=left,
xmin=-4.0, xmax=-3.0, ymin=0.0, ymax=16.0, xtick={-4,-3} ]
\addplot[red] {x^2}; \label{plot_one} \coordinate (SE) at (rel axis cs: 1,0); \coordinate (NE) at (rel axis cs: 1,1); \end{axis}

\begin{axis}[scale only axis, width=1cm, height=4cm, xmin=-4.0, xmax=-3.0, axis y line=none, axis x line=none, ymin=-12, ymax=12.0, ]
\addplot[blue] {x}; \addplot[green] {3x}; \end{axis} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{axis}[xshift=2cm, scale only axis, width=4cm, height=4cm, xlabel={$x$}, axis y line=none,
xmin=0.0, xmax=4.0, ymin=0.0, ymax=16.0, name=right ]
\addplot[red] {x^2}; \label{plot_one} \coordinate (SW) at (rel axis cs: 0,0); \coordinate (NW) at (rel axis cs: 0,1); \end{axis} \begin{axis}[xshift=2cm, scale only axis, width=4cm, height=4cm, ylabel={Ordinate 2}, xmin=0.0, xmax=4.0, axis y line
=right, axis x line=none, ymin=-12, ymax=12.0, ]
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1} \addplot[blue] {x}; \label{plot_two} \addplot[green] {3*x}; \label{plot_three} \addlegendentry{plot 2} \addlegendentry{plot 3} \end{axis}

\path (7cm,0);% kludge to fix bounding box \draw[red] (SE) -- (SW); \draw[red] (NE) -- (NW);

\end{tikzpicture} \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • The lines do not match on both y axes when compared to the original plot without the axis break. – Sid Feb 15 '21 at 18:08