为线段分配不同的颜色

为线段分配不同的颜色

我有一条由三段构成的曲线,我想为它分配不同的颜色。

但当前代码中,最后一个命令[green]获胜。

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale=.15]
  \definecolor{red}{HTML}{F00000}
  \definecolor{blue}{HTML}{0000F0}
  \definecolor{green}{HTML}{008000}

  \coordinate (P1) at (6,6);
  \coordinate (P2) at (22,30);
  \coordinate (P3) at (42,30);
  \coordinate (P4) at (70,24);

  \coordinate (C1) at (11.16,16);
  \coordinate (C2) at (16.31,26);
  \coordinate (C3) at (27.69,34);
  \coordinate (C4) at (33.91,32);
  \coordinate (C5) at (50.09,28);
  \coordinate (C6) at (60.04,26);

   \draw[line width=2]
        (P1) [red] .. controls (C1) and (C2) ..
        (P2) [blue] .. controls (C3) and (C4) ..
        (P3) [green] .. controls (C5) and (C6) ..
        (P4);
\end{tikzpicture}
\end{document}

输出: 在此处输入图片描述

更新

我使用下面的答案并希望修复连接流畅的问题。因此更新一个新示例,该示例更明显地显示了使用三个绘制命令时的流畅问题。

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[]
  \definecolor{red}{HTML}{F00000}
  \definecolor{blue}{HTML}{0000F0}
  \definecolor{green}{HTML}{008000}

  \coordinate (P1) at (0,0);
  \coordinate (P2) at (2,2);
  \coordinate (P3) at (3,1);
  \coordinate (P4) at (5,3);

   \draw[line width=10, red] (P1) -- (P2);
   \draw[line width=10, blue] (P2) -- (P3);
   \draw[line width=10, green] (P3) -- (P4);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

只需用不同的颜色多次绘制完全相同的东西 - 就可以得到完美平滑的曲线。

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[ultra thick]
\begin{scope}
\clip (0,0) rectangle (1,3);
\draw[red] plot[smooth] coordinates {(0,0) (1,2) (3,1) (4,3)};
\end{scope}
\begin{scope}
\clip (1,0) rectangle (3,3);
\draw[green] plot[smooth] coordinates {(0,0) (1,2) (3,1) (4,3)};
\end{scope}
\begin{scope}
\clip (3,0) rectangle (4,3);
\draw[blue] plot[smooth] coordinates {(0,0) (1,2) (3,1) (4,3)};
\end{scope}
\end{tikzpicture}
\end{document}

红、绿、蓝的平滑曲线

\clip用于仅显示剪切路径中的部分 - 这里是一个矩形。

答案2

可能像这样

在此处输入图片描述

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale=.15]
  \definecolor{red}{HTML}{F00000}
  \definecolor{blue}{HTML}{0000F0}
  \definecolor{green}{HTML}{008000}

  \coordinate (P1) at (6,6);
  \coordinate (P2) at (22,30);
  \coordinate (P3) at (42,30);
  \coordinate (P4) at (70,24);

  \coordinate (C1) at (11.16,16);
  \coordinate (C2) at (16.31,26);
  \coordinate (C3) at (27.69,34);
  \coordinate (C4) at (33.91,32);
  \coordinate (C5) at (50.09,28);
  \coordinate (C6) at (60.04,26);

   \draw[line width=2, red] (P1) .. controls (C1) and (C2) .. (P2);
   \draw[line width=2, blue] (P2) .. controls (C3) and (C4) .. (P3);
   \draw[line width=2, green] (P3) .. controls (C5) and (C6) .. (P4);
\end{tikzpicture}
\end{document}

相关内容