1

I'm following the example from Axis break in pgfplots

However, I found that you get a spurious vertical space on the plots

enter image description here

Any way to remove it? And why is it there?

\documentclass[convert]{standalone}  
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\pgfplotsset{every non boxed x axis/.style={}}
\fbox{\begin{tikzpicture}%
\begin{groupplot}[
    group style={
        group name=my fancy plots,
        group size=1 by 2,
        xticklabels at=edge bottom,
        vertical sep=0pt
    },
    width=8.5cm,
    xmin=-6, xmax=6
]%
%
\nextgroupplot[ymin=45,ymax=80,
               ytick={60,80},
               axis x line=top, 
               axis y discontinuity=parallel,
               height=4.5cm]%
\addplot {x*0};%
\addplot {x^2+50};%
%
\nextgroupplot[ymin=0,ymax=5,
               ytick={0},
               axis x line=bottom,
               height=2.0cm]%
\addplot {x*0};% 
\addplot {x^2+50};%
\end{groupplot}%
\end{tikzpicture}}

\end{document}
adn
  • 11,233

2 Answers2

2

I'd add these plots only once, but it may very well be that I am missing something.

\documentclass[convert,border=3.14mm]{standalone}  
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\pgfplotsset{every non boxed x axis/.style={}}
\fbox{\begin{tikzpicture}%
\begin{groupplot}[
    group style={
        group name=my fancy plots,
        group size=1 by 2,
        xticklabels at=edge bottom,
        vertical sep=0pt
    },
    width=8.5cm,
    xmin=-6, xmax=6
]%
%
\nextgroupplot[ymin=45,ymax=80,
               ytick={60,80},
               axis x line=top, 
               axis y discontinuity=parallel,
               height=4.5cm]%
%\addplot {x*0};%
\addplot[red,mark=square*] {x^2+50};%
%
\nextgroupplot[ymin=0,ymax=5,overlay,
               ytick={0},
               axis x line=bottom,
               height=2cm]%
\addplot[blue,mark=*] {x*0};% 
%\addplot {x^2+50};%
\end{groupplot}%
\path(current axis.south) -- ++(0,-0.5cm);
\end{tikzpicture}}

\end{document}

enter image description here

  • The problem removing the plots is that later I will need to handle the legends some how. I won't get all of the plots on the same legend. As it is now, it is simulating a single plot with the discontinuity. – adn Sep 26 '18 at 22:22
  • 1
    @adn There exist \addlegendimage and \addlegendentry for that purpose. But I can see that any of these tricks can backfire once one adds another layer of sophistication. –  Sep 26 '18 at 22:42
2

My solution needed to use the repeated plots to ease further computations. My solution was to do a work around, similar to the \useasboundingbox option suggested by @Henri Menke.

However, the bounding box didn't work for me out of the box. Resetting the bounding box was needed for me with \pgfresetboundingbox\path (plot c1r1.outer north west) rectangle (plot c1r2.outer south east);, where the plot is the name of the groups and the c1r1 is the naming convention for the group.

enter image description here

\documentclass[convert]{standalone}  
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\pgfplotsset{every non boxed x axis/.style={}}
\fbox{\begin{tikzpicture}[]%
\begin{groupplot}[
    group style={
        group name=plot,
        group size=1 by 2,
        xticklabels at=edge bottom,
        vertical sep=0pt
    },
    width=8.5cm,
    xmin=-6, xmax=6
]%
%
\nextgroupplot[ymin=45,ymax=80,
               ytick={60,80},
               axis x line=top, 
               axis y discontinuity=parallel,
               height=4.5cm,
               legend to name=leg
               ]%
\addplot {x*0};%
\addplot {x^2+50};%
\nextgroupplot[ymin=0,ymax=5,
               ytick={0},
               axis x line=bottom,
               height=2.0cm,
               legend to name=leg
               ]%
\addplot {x*0};
\addplot {x^2+50};%
\end{groupplot}%
\pgfresetboundingbox\path (plot c1r1.outer north west) rectangle (plot c1r2.outer south east); % adjust to fit
\end{tikzpicture}}

\end{document}
adn
  • 11,233