如何在特定位置绘制函数(例如:矩形内)

如何在特定位置绘制函数(例如:矩形内)

我有两个矩形,我想在每个矩形内绘制一个函数。

有没有办法定位图?它似乎默认为 (0,0),我不确定如何添加位置偏移。(让红色正弦波位于块 B2 的中心)

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows, positioning}
\begin{document}
\begin{tikzpicture}[node distance=5mm, 
       blockcolors/.style={
        % The rest
        thick,draw=black,
        top color=white,
        bottom color=black!10,
        font=\sffamily\small
    },
    blockheight/.style = {
        minimum height=10mm
    },
    block/.style={
        % The shape:
        rectangle, minimum size=6mm, minimum width=12mm,
        blockheight,
        node distance=5mm,
        blockcolors,
        drop shadow
    }
]
\node [block] (B1) at (0,0) {};
\node [block, right=10mm of B1] (B2) at (0,0) {};

\draw[scale=0.4,domain=-1:1,smooth,variable=\x,blue] plot ({\x},{sin(180 * \x)});
\draw[scale=0.4,domain=-1:1,smooth,variable=\x,red] plot ({\x},{sin(180 * 2*\x)});


\end{tikzpicture}
\end{document}

答案1

您需要做的就是添加shift={(B2.center)}到第二个图。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows, positioning}
\begin{document}
\begin{tikzpicture}[node distance=5mm, 
       blockcolors/.style={
        % The rest
        thick,draw=black,
        top color=white,
        bottom color=black!10,
        font=\sffamily\small
    },
    blockheight/.style = {
        minimum height=10mm
    },
    block/.style={
        % The shape:
        rectangle, minimum size=6mm, minimum width=12mm,
        blockheight,
        node distance=5mm,
        blockcolors,
        drop shadow
    }
]
\node [block] (B1) at (0,0) {};
\node [block, right=10mm of B1] (B2) at (0,0) {};

\draw[scale=0.4,domain=-1:1,smooth,variable=\x,blue] plot ({\x},{sin(180 * \x)});
\draw[shift={(B2.center)},scale=0.4,domain=-1:1,smooth,variable=\x,red] plot ({\x},{sin(180 * 2*\x)});


\end{tikzpicture}
\end{document}

在此处输入图片描述

您还可以使用path picture或定义pic

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shadows.blur}
\begin{document}
\begin{tikzpicture}[pics/graph/.style={code={%
\draw[thick,top color=white, bottom color=black!10,blur shadow] (-0.6,-0.5) rectangle (0.6,0.5);
#1}}]

 \path pic{graph={\draw[scale=0.4,domain=-1:1,smooth,variable=\x,blue] plot ({\x},{sin(180 * \x)});}}
 (1.5,0) pic{graph={\draw[scale=0.4,domain=-1:1,smooth,variable=\x,red] plot ({\x},{sin(180 * 2*\x)});}};
\end{tikzpicture}    
\end{document}

在此处输入图片描述

相关内容