# Create a pile of two graphs using TikZ

For a report in my school course, I need to make an analysis of an output signal from a certain Electrical circuit according to the input. Here is the graph which I am trying to reproduce:

It consists of an two graphs which are stacked up, one on top of another. I actually looked up on texample.net in order to try it by myself but I just quite couldn't get it right. I created two {tikzpicture} blocks in order to get it but I failed to make it when it comes to drawing the red dashed vertical lines from the inputs all the way to the outputs. I looked at these two examples:

• Could you perhaps post the code for the two unconnected graphs? This would make it much easier to come up with a nice solution. This answer explains how to connect two different tikzpictures. Nov 15 '19 at 10:31
• Sure, I will fine tune the two unconnected graphs and will post them in question. Nov 15 '19 at 10:34
• If these graphs are done with pgfplots, you may want to have a look at the groupplots library.
– user194703
Nov 15 '19 at 16:00
• @Schrödinger'scat indeed these are drawn using pgfplots, but I am not proficient in TikZ so I am checking out that groupplots library that you mentionned from the PDF manual. Nov 15 '19 at 16:10
• There are many explicit examples, and here is one that I wrote a few days ago. You may use it to see how one can connect lines across plots.
– user194703
Nov 15 '19 at 16:34

Here is a proposal that derives the lower plot from the intersections of the upper plot with the y=2 and y=-2 lines.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}% <- if you have an older installation, try 1.15 or 1.14
\usepgfplotslibrary{groupplots,fillbetween}
\begin{document}
\begin{tikzpicture}[node font=\sffamily]
\begin{groupplot}[group style={group size=1 by 2,vertical sep=4.5em},
width=9cm,height=5cm,
xmin=0,xmax=0.035,ymin=-5.2,ymax=5.2,
xtick={0,0.005,...,0.035},grid=major,
domain=0:0.035,xlabel={},axis on top,samples=201,
scaled x ticks=false,/pgf/number format/fixed,
x tick label style={font=\small},y tick label style={font=\small}]
\nextgroupplot[title={Input signal},
ylabel={Input signal voltage},ytick={-5,-2,2,5}]
\path[name path=upper] (\pgfkeysvalueof{/pgfplots/xmin},2)
-- (\pgfkeysvalueof{/pgfplots/xmax},2);
\path[name path=lower] (\pgfkeysvalueof{/pgfplots/xmin},-2)
-- (\pgfkeysvalueof{/pgfplots/xmax},-2);
\path[name intersections={of={sine and upper},name=u},
name intersections={of={sine and lower},name=l}];
\path (\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax})
coordinate (TR1);
\nextgroupplot[title={Schmitt output signal},
ytick={-5,-3,3,5},ylabel={Schmitt trigger voltage output}]
\draw[cyan,thick] (0,-3) -| (u-1|-0,3) coordinate (U-1)
-| (l-1|-0,-3) coordinate (L-1) -| (u-3|-0,3) coordinate (U-3)
-| (l-3|-0,-3) coordinate (L-3)-- (0.035,-3);
\path (\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax})
coordinate (TR2);
\end{groupplot}
\draw[red,dashed] foreach \X in {1,3} {(u-\X) -- (U-\X)};
\draw[dashed] foreach \X in {1,3} {(l-\X) -- (L-\X)};
\path foreach \X in {1,2}
{(TR\X) node[anchor=north east,align=right,font=\small]
{$V_\mathrm{OL} = 3\mathrm{V}$\\
$V_\mathrm{HL} = 3\mathrm{V}$}};
\end{tikzpicture}
\end{document}


• Thanks a million for sharing that example from one of your responses. I will learn it from now on. Just one last request: Is it possible to make the numbers appear smaller (I think bigger numbers kind of clutter the graph, but is only a personal preference)? And add $V_{OL} = 3V$ and $V_{HL} = 3V$ on the upper-right part of the boxes? Nov 15 '19 at 17:23
• @billyandriam You are welcome! I added x tick label style={font=\small},y tick label style={font=\small} to make them smaller.
– user194703
Nov 15 '19 at 17:30