我想在 TikZ 节点内添加图。这是我已有的代码的最小工作示例。
\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}
MWE 创建了下图:
我想在框图中使用多个这样的节点。是否可以将示波器替换为具有轴线和固定宽度和高度的常规轴,以便简单地将绘图替换为其他绘图而不影响轴的外观?
答案1
修改后的版本,框架作为样式的一部分进行绘制simple axis
,并且该样式的参数允许您为框架节点指定名称。可以使用库相对于其他节点进行定位calc
,但不使用该库的便捷语法positioning
(据我所知)。
\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}
旧答案
您在寻找类似的东西吗?
\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
我可以自己更改 Torbjørn T 的解决方案。我将轴的锚点改为中心,并将其放置在节点的位置。
\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}