有没有办法将第二个图放在第一个图的“此处”位置?
梅威瑟:
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
%% First plot
\begin{tikzpicture}[scale=1.5, domain=0:2*pi]
\draw[very thin,color=gray] (0, 2*pi);
\draw[->] (0,0) -- (2*pi,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$y$};
\draw[color=blue] plot (\x,{sin(\x r)});
\node[below left, red] at (0, -1.2) {HERE};
\end{tikzpicture}
% Second plot
\begin{tikzpicture}[scale=1.5, domain=0:2*pi, rotate=-90]
\draw[very thin,color=gray] (0, 2*pi);
\draw[->] (0,0) -- (2*pi,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$y$};
\draw[color=blue] plot (\x,{cos(\x r)});
\end{tikzpicture}
\end{document}
答案1
您可以将第二个图放在scope
第一个图内tikzpicture
,然后shift
将其放在您想要的位置。可能需要调整偏移值,我不知道您到底想要什么。通过围绕原点旋转,可能更容易找出偏移值。
但是边界框存在问题,图上方添加了很多空白区域。您可以通过保存合适的角坐标、重置边界框,然后使用保存的坐标定义新的边界框来解决这个问题。
\documentclass[12pt,border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
%% First plot
\begin{tikzpicture}[scale=1.5, domain=0:2*pi]
\draw[very thin,color=gray] (0, 2*pi);
\draw[->] (0,0) -- (2*pi,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$y$};
\draw[color=blue] plot (\x,{sin(\x r)});
\node[below left, red] at (0, -1.2) {HERE};
% Second plot
\begin{scope}[rotate around={-90:(0,0)},shift={(1.2cm,0)}]
\draw[very thin,color=gray] (0, 2*pi);
\draw[->] (0,0) -- (2*pi,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$y$};
\draw[color=blue] plot (\x,{cos(\x r)});
\end{scope}
\coordinate (upperright) at (7,2);
\coordinate (lowerleft) at (current bounding box.south west);
\pgfresetboundingbox
\useasboundingbox (upperright) rectangle (lowerleft);
\draw (current bounding box.south east) rectangle (current bounding box.north west);
\end{tikzpicture}
\end{document}
通过在图中切换 x 和 y 坐标并更改轴的绘制,可以获得类似的结果。这里也需要一些边界框调整。
\documentclass[12pt,border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
%% First plot
\begin{tikzpicture}[scale=1.5, domain=0:2*pi]
\draw[very thin,color=gray] (0, 2*pi);
\draw[->] (0,0) -- (2*pi,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$y$};
\draw[color=blue] plot (\x,{sin(\x r)});
\node[below left, red] at (0, -1.2) {HERE};
% Second plot
\begin{scope}[yshift=-1.2cm]
\draw[very thin,color=gray] (-2*pi,0);
\draw[->] (0,0) -- (0,-2*pi) node[right] {$x$};
\draw[->] (-1.2,0) -- (1.2,0) node[above] {$y$};
\draw[color=blue] plot ({cos(\x r)},-\x);
\end{scope}
\coordinate (upperleft) at (-1.5,2);
\coordinate (lowerright) at (current bounding box.south east);
\pgfresetboundingbox
\useasboundingbox (upperleft) rectangle (lowerright);
\draw (current bounding box.south east) rectangle (current bounding box.north west);
\end{tikzpicture}
\end{document}
答案2
这不是对直接问题的回答,而是使用sin
和cos
路径映射正弦/余弦值的简单说明。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x in {-pi/2,0,pi/2}{
\draw[dashed,red!30] (\x,0) -- ++(0,2*pi);
\draw[dashed,red!30] (0,-\x) -- ++(-2*pi,0);
}
\draw (0,0) circle (pi/2);
\begin{scope}[shift={(-pi/2,1.5*pi)}]
\draw (0,0) node[left] {0}-- (pi,0) (0,1) node[left] {1};
\draw[ultra thick,blue] (pi,1) cos (pi/2,0) sin (0,-1) node[left] {-1} ;
\end{scope}
\begin{scope}[rotate=-90,shift={(-pi/2,-1.5*pi)}]
\draw (0,0) node[above] {0}-- (pi,0) (0,-1) node[above] {-1};
\draw[ultra thick,blue] (0,1) node[above] {1} cos (pi/2,0) sin (pi,-1);
\end{scope}
\end{tikzpicture}
\end{document}
我不知道实际意图,但它可以通过在圆上放置任意点来实现,并且可以延伸线以与曲线相交等。