4

I would like to add plots inside TikZ nodes. Here is a minimum working example of the code I already have.

\documentclass[]{standalone}
\usepackage{pgf,tikz,pgfplots}
\usetikzlibrary{calc,positioning}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\draw node[draw,minimum width=2cm, minimum height=15mm},label=Test] {};
\begin{scope}[x=0.016cm,y=8,shift={(-0.8cm,0cm)}]
\draw[thick] (0,0) plot[domain=0:100,samples=30,mark=none] ({\x},{sin(2*pi*\x)});
\end{scope}
\end{tikzpicture}
\end{document}

The MWE creates the following figure:

Axis in TikZ node

I would like to use multiple of these nodes in a block diagram. Is it possible to replace the scope with a regular axis with axis lines and a fixed width and height to simply replace the plot with a different one without affecting the appearance of the axis?

2 Answers 2

5

Modified version, a frame is drawn as part of the simple axis style, and an argument to that style lets you specify a name for the frame node. Positioning can be done relative to other nodes with the calc library, but not using the convenient syntax of the positioning library (as far as I know).

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots} % loads tikz which loads pgf
\usetikzlibrary{calc,fit}
\pgfplotsset{
  compat=1.15,
  simple axis/.style={
    scale only axis,
    anchor=center,
    axis lines=middle,
    enlargelimits=0.1,
    width=2cm,
    height=15mm,
    xtick=\empty,
    ytick=\empty,
    domain=0:100,
    tickwidth=0,
    clip mode=individual,
    every axis plot/.append style={smooth},
    cycle list={
      black, thick\\
      black, thick, dashed \\
    },
    before end axis/.code={
        \node [draw,fit=(current axis)] (#1) {};
        }
    },
  simple axis/.default=foo
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ % no position defined, so this ends up at (0,0)
  simple axis=ax1,
  title=Test,
  ]
  \addplot  {sin(2*pi*x)};

\end{axis}

\begin{axis}[
  simple axis=ax2,
  title=Test2,
  at={($(ax1.east)+(1cm,0)$)}, % specify position relative to east anchor of ax1
  anchor=west
  ]
  \addplot {cos(2*pi*x)};
\end{axis}


\draw [red, thick, -stealth] (ax1) -- (ax2);

\end{tikzpicture}
\end{document}

Old answer

Are you after something like this?

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots} % loads tikz which loads pgf
\usetikzlibrary{calc,positioning}
\pgfplotsset{
  compat=1.15,
  simple axis/.style={
    scale only axis,
    width=2cm,
    height=15mm,
    xtick=\empty,
    ytick=\empty,
    domain=0:100,
    tickwidth=0
    }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ % no position defined, so this ends up at (0,0)
  simple axis,
  title=Test,
  ]
  \addplot [thick, smooth] {sin(2*pi*x)};
\end{axis}


\begin{axis}[
  simple axis,
  title=Test2,
  at={(3cm,1cm)} % specify position
  ]
  \addplot [thick, smooth] {cos(2*pi*x)};
\end{axis}
\end{tikzpicture}
\end{document}
2
  • This almost looks perfect, thank you. Is it possible to add these axis inside the nodes, so I can simply make a block diagram out of it? I would also like to have the arrows for the axis lines as well as the box around it.
    – brnk
    Jun 17, 2019 at 15:35
  • @brnk I updated my answer a bit, perhaps some of the modifications are of interest. Jun 17, 2019 at 16:51
1

I was able to change Torbjørn T's solution by myself. I changed the axis' anchor to center and placed it at the node's position.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots} % loads tikz which loads pgf
\usetikzlibrary{calc,positioning}
\pgfplotsset{
  compat=1.15,
   simple axis/.style={
    scale only axis,
    width=1.7cm,
    height=12mm,
    xtick=\empty,
    ytick=\empty,
    domain=0:100,
    tickwidth=0,
    axis lines=left,
    enlargelimits=upper,
    anchor=center,
    }
}

\begin{document}
\begin{tikzpicture}
\draw node[draw,minimum width=2cm, minimum height=15mm,label=Test] (node1) {};
\begin{axis}[simple axis,at={(node1)}]
  \addplot [thick, smooth] {sin(2*pi*x)};
\end{axis}

\draw node[draw,minimum width=2cm, minimum height=15mm,label=Test 2,right=of node1] (node2) {};
\begin{axis}[simple axis,at={(node2)}]
  \addplot [thick, smooth] {sin(2*pi*x)};
\end{axis}

\draw[->] (node1)-- (node2);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .