2

I would like to draw another chart over an existing one to take advantage of an unused area. I'm getting close to what I wanted by using a negative baseline and hskip. Unfortunately, the latter messes up the horizontal alignment, which could be fixed by using a box, perhaps? Also, controlling the final position is done by eyeballing.

Anyhow, is there a more elegant way to do what I'm after? I've Googled extensively, but it appears like plot overlays were never discussed before.

\documentclass[a4paper]{article}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\usepackage[show frame]{geometry}  % show text edges
\begin{document}
\begin{figure}[t!]\centering
  \begin{tikzpicture}\begin{axis}
    \addplot+ [domain=0:360, samples=101, mark=none] {sin(x)};
  \end{axis}\end{tikzpicture}    
\end{figure}
\begin{figure}[t!]\centering
  \begin{tikzpicture}\begin{axis}
    \addplot+ [domain=0:360, samples=101, mark=none] {sin(x)};
  \end{axis}\end{tikzpicture}\hskip -7cm
  \begin{tikzpicture}[baseline=-1cm]\begin{axis}[width=120pt]
    \addplot+ [domain=0:360, samples=101, mark=none] {sin(2*x)/2};
  \end{axis}\end{tikzpicture}
\end{figure}
\hfill  % so the figures move up
\end{document}

misaligned overlayed charts

Atcold
  • 1,727

1 Answers1

3

If you want something placed relative to something else, then it would be a lot easier to keep it in the same tikzpicture. Here is an example where the second plot is place at a specific coordinate in the first plot:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+ [domain=0:360, samples=101, mark=none] {sin(x)};
\coordinate (subplot) at (20,-0.9);
\end{axis}
\begin{axis}[width=120pt, at=(subplot), tick label style={font=\small}]
\addplot+ [domain=0:360, samples=101, mark=none] {sin(2*x)/2};
\end{axis}
\end{tikzpicture}
\end{document}

One plot inside an other

You can also just place the second plot at some coordinate in the TikZ picture or relative to the corner of the first plot.

  • 1
    This is just so awesome. I did find the at command just now in the documentation. Here they do something even fancier, and use the name TikZ keyword (not even mentioned in pgfplots manual). Anyhow, thanks for the helping with this and for the \coordinate trick! – Atcold Jun 02 '22 at 22:17
  • 1
    Here is also an example on this site: https://tex.stackexchange.com/a/644047/8650 – hpekristiansen Jun 03 '22 at 09:12