如何将图像节点划分为相等的部分并为每个部分绘制相应的线条

如何将图像节点划分为相等的部分并为每个部分绘制相应的线条

我需要帮助在我想注释的图表下方制作 65 条自定义线条。我想将每三行涂成相同的颜色,直到第 30 行,然后重复。这是我目前所做的,它有效,但效率很低,而且每行输入都需要很长时间。我认为一个\foreach命令就足够了,但我搞不懂。请帮忙。谢谢。

\documentclass{article}

\usepackage{graphicx,tikz}
\usetikzlibrary{calc,backgrounds}
\tikzstyle{background grid}=[draw, black!50,step=1cm]

\begin{document}
\begin{tikzpicture}[show background grid]
\node[anchor=south west,inner sep=0] (gradient) at (0,0) {\includegraphics[width=\textwidth]{gradient.png}};
%\fill (0,0) circle (2pt);
\draw[red] let \p1=($(gradient.east)!0.946!(gradient.west)$) in (\x1,0) node(one)[below] {} -- (\x1,-1);
\draw[red] let \p1=($(gradient.east)!0.932!(gradient.west)$) in (\x1,0) node(two)[below] {} -- (\x1,-1);
\draw[red] let \p1=($(gradient.east)!0.918!(gradient.west)$) in (\x1,0) node(three)[below] {} -- (\x1,-1);
\draw[blue] let \p1=($(gradient.east)!0.904!(gradient.west)$) in (\x1,0) node(four)[below] {} -- (\x1,-1);
\draw[blue] let \p1=($(gradient.east)!0.89!(gradient.west)$) in (\x1,0) node(five)[below] {} -- (\x1,-1);
\draw[blue] let \p1=($(gradient.east)!0.876!(gradient.west)$) in (\x1,0) node(six)[below] {} -- (\x1,-1);
\draw[orange] let \p1=($(gradient.east)!0.862!(gradient.west)$) in (\x1,0) node(seven)[below] {} -- (\x1,-1);
\draw[orange] let \p1=($(gradient.east)!0.848!(gradient.west)$) in (\x1,0) node(eight)[below] {} -- (\x1,-1);
\draw[orange] let \p1=($(gradient.east)!0.834!(gradient.west)$) in (\x1,0) node(nine)[below] {} -- (\x1,-1);
\draw[purple] let \p1=($(gradient.east)!0.820!(gradient.west)$) in (\x1,0) node(ten)[below] {} -- (\x1,-1);
\draw[purple] let \p1=($(gradient.east)!0.806!(gradient.west)$) in (\x1,0) node(eleven)[below] {} -- (\x1,-1);
\draw[purple] let \p1=($(gradient.east)!0.792!(gradient.west)$) in (\x1,0) node(twelve)[below] {} -- (\x1,-1);

\draw[red] let \p1=($(gradient.east)!0.386!(gradient.west)$) in (\x1,0) node(thirtyone)[below] {} -- (\x1,-1);
\draw[red] let \p1=($(gradient.east)!0.372!(gradient.west)$) in (\x1,0) node(thirtytwo)[below] {} -- (\x1,-1);
\draw[red] let \p1=($(gradient.east)!0.358!(gradient.west)$) in (\x1,0) node(thirtythree)[below] {} -- (\x1,-1);


\end{tikzpicture}
%
\end{document}

在此处输入图片描述

答案1

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,backgrounds}
\tikzset{background grid/.style={draw, black!50,step=1cm}}
\colorlet{color0}{red}
\colorlet{color1}{blue}
\colorlet{color2}{orange}
\colorlet{color3}{purple}
\colorlet{color4}{cyan}
\colorlet{color5}{green}
\colorlet{color6}{brown}
\colorlet{color7}{olive}
\colorlet{color8}{pink}
\colorlet{color9}{magenta}
\begin{document}
\begin{tikzpicture}%[show background grid]
  \node[anchor=south west,inner sep=0] (gradient) at (0,0)
    {\includegraphics[width=\textwidth]{example-image}};
  \def\nl{64}% number of lines minus 1, number of gaps
  \foreach \i in {0,...,\nl}
    {\pgfmathtruncatemacro\c{divide(mod(\i,30),3)}%
     \draw[color\c]
       ($(gradient.south west)!{\i/\nl}!(gradient.south east)$)
       -- +(0,-1) coordinate (l-\i);
    }
  \draw (l-23)
    node{$\bullet$}
    node[below]{dot is at 23rd line, counting starts left with 0};
\end{tikzpicture}
\end{document}

答案2

像这样吗?

65 行

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{backgrounds}
\tikzset{% \tikzstyle is deprecated
  background grid/.style={draw, black!50, step=1cm},
}

\begin{document}
\begin{tikzpicture}[show background grid]
  \node [inner ysep=0, inner xsep=-\pgflinewidth] (gradient) {\includegraphics[width=\textwidth]{example-image-a}};
  \begin{scope}[shift=(gradient.south west), x=(gradient.south east)]
    \foreach \i [evaluate=\i as \j using \i/64, evaluate=\i as \k using {int(mod(\i,30))}] in {0,...,64}
    {
      \ifnum\k<3\def\tempcolour{red}\else\ifnum\k<6\def\tempcolour{blue}\else\ifnum\k<9\def\tempcolour{orange}\else\ifnum\k<12\def\tempcolour{purple}\else\ifnum\k<15\def\tempcolour{magenta}\else\ifnum\k<18\def\tempcolour{cyan}\else\ifnum\k<21\def\tempcolour{green}\else\ifnum\k<24\def\tempcolour{yellow}\else\ifnum\k<27\def\tempcolour{gray}\else\def\tempcolour{brown}\fi\fi\fi\fi\fi\fi\fi\fi\fi
      \draw [draw=\tempcolour] (\j,0) -- (\j,-1);
    }
  \end{scope}
\end{tikzpicture}
\end{document}

注意调整要inner xsep确保第一行的左半部分和最后一行的右半部分不超出图像的边界。

相关内容