三角形内的彩色形状

三角形内的彩色形状

如何才能实现类似下面我用 TikZ 实现的效果,并进一步能够使用不同的颜色,甚至在每个较小的形状内放置一个图例?

通缉

答案1

这对于fillbetween图书馆来说是一件容易的事pgfplots.一个简单的例子:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  xmin=100,
  xmax=800,
  ymin=0,
  ymax=600,
  domain=100:800
]
% The main line (the graph of the function)
\addplot+[no marks,black,name path=main line] {0.857*x-85};
% The x axis
\addplot+[no marks,black,name path=xaxis] {0};
% The vertical lines
\draw 
  (axis cs:350,0) -- (axis cs:350,0.857*350-85);
\draw 
  (axis cs:450,0) -- (axis cs:450,0.857*450-85);
\draw 
  (axis cs:650,0) -- (axis cs:650,0.857*650-85);
% The horizontal lines
\draw[name path=hline1] 
  (axis cs:450,250) -- (axis cs:800,250);
\draw[name path=hline2] 
  (axis cs:650,450) -- (axis cs:800,450) coordinate (aux7);
% Filling the regions
\addplot[orange!30] 
  fill between[of=main line and xaxis,soft clip={domain=100:350}];
\addplot[green!80!black!50] 
  fill between[of=main line and xaxis,soft clip={domain=350:450}];
\addplot[magenta!50] 
  fill between[of=main line and hline1,soft clip={domain=450:650}];
\addplot[olive!50] 
  fill between[of=hline1 and xaxis,soft clip={domain=450:650}];
\addplot[cyan!50] 
  fill between[of=main line and hline2,soft clip={domain=650:800}];
\addplot[yellow!50] 
  fill between[of=hline2 and hline1,soft clip={domain=650:800}];
\addplot[red!80!black] 
  fill between[of=hline1 and xaxis,soft clip={domain=650:800}];
\end{axis} 
\end{tikzpicture}

\end{document}

相关内容