我需要在图表顶部绘制两个不同颜色的矩形(或者这可以适用于任何形状)。我拥有的 MWE 是。代码有效,但我无法更改正方形的坐标以准确实现我想要的结果。我一直在使用 进行绘图tikzpicture
。
我的MWE
情况如下:
\documentclass[10pt, compress]{beamer}
\usepackage{tikz}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[width=\textwidth]{images/sample.png}};
\draw[red,ultra thick,rounded corners] (5.3,5.3) rectangle (8.4,8.4);
\draw[green,ultra thick,rounded corners] (3.4,3.4) rectangle (8.3,8.3);
\end{tikzpicture}
\end{frame}
\end{document}
这MWE
正确地在图像顶部绘制了矩形,但它们的位置却不正确。
期望的结果如下:
然而,目前我取得了以下成果:
有人能提供一些见解,告诉我如何最好地估计坐标,以帮助我获得所需的输出吗?这个问题在我完成几项任务时反复出现——所以我想了解如何计算它们。谢谢!
答案1
当我想在图像上绘图时,我首先使用 foreach 和 calc 库按比例绘制图像的网格。
然后我用这些坐标来绘制
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (img)at (0,0) {\includegraphics[width=\textwidth]{sample.png}};
\foreach \xx in {0,1,2,...,9}{%
\draw[dashed] ($(img.north)!0.\xx!(img.north east)$)node[above]{0.\xx} -- ($(img.south)!0.\xx!(img.south east)$);
\draw[dashed] ($(img.east)!0.\xx!(img.north east)$)node[right]{0.\xx} -- ($(img.west)!0.\xx!(img.north west)$);
}
\path ($(img.north)!0.1!(img.north east)$)|-coordinate(c1)($(img.east)!0.3!(img.north east)$);% c1: first corner
\path ($(img.north)!0.82!(img.north east)$)|-coordinate(c2)($(img.east)!0.77!(img.north east)$); %c2 : second corner
\draw[red,ultra thick,rounded corners] (c1) rectangle (c2);
% \draw[green,ultra thick,rounded corners] (3.4,3.4) rectangle (8.3,8.3);
\end{tikzpicture}
使用比例坐标来改变图像的比例并保持正确的绘制
\begin{tikzpicture}[scale=0.5,transform shape]
\node[anchor=south west,inner sep=0] (img)at (0,0) {\includegraphics[width=\textwidth]{sample.png}};
\foreach \xx in {0,1,2,...,9}{
\draw[dashed] ($(img.north)!0.\xx!(img.north east)$)node[above]{0.\xx} -- ($(img.south)!0.\xx!(img.south east)$);
\draw[dashed] ($(img.east)!0.\xx!(img.north east)$)node[right]{0.\xx} -- ($(img.west)!0.\xx!(img.north west)$);
}
\path ($(img.north)!0.1!(img.north east)$)|-coordinate(c1)($(img.east)!0.3!(img.north east)$);
\path ($(img.north)!0.82!(img.north east)$)|-coordinate(c2)($(img.east)!0.77!(img.north east)$);
\draw[red,ultra thick,rounded corners] (c1) rectangle (c2);
\end{tikzpicture}