2

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:

two tikz images, without a dashed vertical line connecting them

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
]
\addplot[black, thick] {1.8 - 0.2*x};
\addplot[black, thick] {3.0 - 0.4*x};
\addplot[black, thick] {2.8 - 0.6*x};
\addplot[black, thick] {3.8 - 0.6*x};
\addplot[black, thick] {3.8 - 0.8*x};
\addplot[black, thick] {5.0 - 1.2*x};
\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):

two tikz images, with a dashed vertical line connecting them

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.

7

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]
\addplot[black, thick] {1.8 - 0.2*x};
\addplot[black, thick] {3.0 - 0.4*x};
\addplot[black, thick] {2.8 - 0.6*x};
\addplot[black, thick] {3.8 - 0.6*x};
\addplot[black, thick] {3.8 - 0.8*x};
\addplot[black, thick] {5.0 - 1.2*x};
\coordinate (a) at (2,\pgfkeysvalueof{/pgfplots/ymax});

\nextgroupplot[title={bottom plot}, samples=100]
\addplot[red, very thick] { 2 * sin(deg(0.5*x + 6)) + 2.5 };
\coordinate (b) at (2,\pgfkeysvalueof{/pgfplots/ymin});
\end{groupplot}

\draw [dashed] (a) -- (b);
\end{tikzpicture}
\end{figure}
\end{document}
|improve this answer|||||

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.