# How do I draw a dashed line that overlays two adjacent tikz plots?

I have tikz code that creates two plots, one above the other, using the scope environment with a shift and two separate axes. The plots look like this:

This is the code that creates them:

\documentclass[12pt,a4paper]{article}

\usepackage{float}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\def\maxX{10}
\def\maxY{5}

\begin{figure}[H]
\begin{tikzpicture}

\begin{scope}
\begin{axis}[
width=20cm, height=10cm,
title={Top plot}, xlabel={}, ylabel={}, xticklabels={,,}, yticklabels={,,}, axis lines=middle,
domain=0:10,
samples=100,
xmin=0, xmax=\maxX,
ymin=0, ymax=\maxY
]
\end{axis}
\end{scope}

\begin{scope}[yshift=-10cm]
\begin{axis}[
width=20cm, height=10cm,
title={Bottom plot}, xlabel={}, ylabel={}, xticklabels={,,}, yticklabels={,,}, axis lines=middle,
domain=0:10,
samples=200,
xmin=0, xmax=\maxX,
ymin=0, ymax=\maxY
]
\addplot[red, very thick] { 2 * sin(deg(0.5*x + 6)) + 2.5 };
\end{axis}
\end{scope}

\end{tikzpicture}
\end{figure}
\end{document}


I'm trying to draw a dashed vertical line from the top of the top plot to the bottom of the bottom of the bottom plot, like this (made in Gimp):

How do I do this? I tried:

1. Adding a new axis with a vertical line drawn using coordinates, i.e. \draw[dashed] (axis cs:2,-10) -- (axis cs:2,10);, but this scales the plots according to that axis and messes up the spacing in the other two plots

2. Adding that same code, \draw[dashed] (axis cs:2,-10) -- (axis cs:2,10); to one of the current axes, but since they're clipped from 0 to 10, the dashed line doesn't extend the entire way

3. Outside of any axis, using a plain draw command: \draw[dashed] (2, -10) -- (2, 8.5); Since this is outside of an axis, it seems to use different coordinates, so the line is in the wrong horizontal position compared to the other two plots.

Add \coordinate (a) at (2,\pgfkeysvalueof{/pgfplots/ymax}); in the first axis environment and \coordinate (b) at (2,\pgfkeysvalueof{/pgfplots/ymin}); in the second. Then after the last \end{scope}, add \draw [dashed] (a) -- (b);.

As a sidenote, you might be interested in the groupplots library for aligned axes like this.

\documentclass[12pt,a4paper]{article}

\usepackage{float}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{groupplots}
\begin{document}
% in general \newcommand over \def
\newcommand\maxX{10}
\newcommand\maxY{5}

\begin{figure}[H]
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 2,
vertical sep=1.5cm
},
width=20cm, height=10cm,
xlabel={}, ylabel={},
xticklabels={,,}, yticklabels={,,},
axis lines=middle,
domain=0:10,
xmin=0, xmax=\maxX,
ymin=0, ymax=\maxY
]

\nextgroupplot[title={Top plot}, samples=2]
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.865632951259613, "perplexity": 3218.6644585058216}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-16/segments/1585370506673.7/warc/CC-MAIN-20200402045741-20200402075741-00315.warc.gz"}