TikZ 金字塔层次颜色

TikZ 金字塔层次颜色

我有一个 TiZ 金字塔层次结构,但是我想知道是否有办法用颜色填充不同的层。我当前的代码是:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}

\begin{tikzpicture}
\coordinate (A) at (-5,0) {};
\coordinate (B) at ( 5,0) {};
\coordinate (C) at (0,7) {};
\draw[name path=AC] (A) -- (C);
\draw[name path=BC] (B) -- (C);
\foreach \y/\A in {0/Non-League, 1.5/League Two,2.5/ League One,3.5/Championship,4.5/Premier League} {
    \path[name path=horiz] (A|-0,\y) -- (B|-0,\y);
    \draw[name intersections={of=AC and horiz,by=P},
          name intersections={of=BC and horiz,by=Q}] (P) -- (Q)
        node[midway,above] {\A};
}
\end{tikzpicture}
\end{document}

答案1

有两种方法可以进行填充,但实际上还有一种更简单的方法来绘制金字塔。我们基本上定义一个三角形来剪切我们定位的所有填充矩形。再加上 foreach 的最后一个元素的额外填充。

第一种方法包括数组中的颜色列表。我们count在 foreach 中添加一个,并使用它来选择颜色。由于数组具有自动数字索引0,1,2,3,...,因此我们使用了计数。

第二种方法是使用计数来绘制渐变,这要归功于evaluate将计数乘以某个数字的选项。现在,如果你有 10 个矩形,最好这样做,count*10每个矩形都是

color1!0!color2
color1!10!color2
color1!20!color2
color1!30!color2

等等。但你可以选择任意数字,只要你对结果满意即可。对于最后一种解决方案,你当然也可以使用单一颜色,color!##也就是 的缩写,color!##!white这样数字越高,它与白色的混合就越多。

输出

在此处输入图片描述

代码

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,backgrounds}
\begin{document}

% List of colors
\begin{tikzpicture}
\def\colorlist{{"cyan", "red", "orange", "yellow", "green", "gray", "blue", "violet"}}

\foreach \y/\A [count=\xi starting from 0, evaluate=\y as \nexty using (\y+1.5, evaluate=\xi as \grad using int(\xi*15)] in {0/Non-League, 1.5/League Two,2.5/ League One,3.5/Championship,4.5/Premier League} {%
    \pgfmathsetmacro\myfill{\colorlist[\xi]}
    \begin{scope}[on background layer]
    \clip[preaction={draw}] (-5,0) -- (5,0) -- (0,7) -- cycle;
    \fill[\myfill] (-5,\y) rectangle (5,\nexty);
    \fill[\myfill] (-5,4.5) rectangle (5,7);
    \end{scope}
    \node at (0,\y+.3) {\A};
}
\end{tikzpicture}

% Gradations
\begin{tikzpicture}[xshift=5cm]
\foreach \y/\A [count=\xi starting from 0, evaluate=\y as \nexty using (\y+1.5, evaluate=\xi as \grad using int(\xi*15)] in {0/Non-League, 1.5/League Two,2.5/ League One,3.5/Championship,4.5/Premier League} {%
    \begin{scope}[on background layer]
    \clip[preaction={draw}] (-5,0) -- (5,0) -- (0,7) -- cycle;
    \fill[red!\grad!yellow] (-5,\y) rectangle (5,\nexty);
    \fill[red!\grad!yellow] (-5,4.5) rectangle (5,7);
    \end{scope}
    \node at (0,\y+.3) {\A};
}
\end{tikzpicture}
\end{document}

答案2

使用简单的三角形路径,我们可以根据\i下面循环的计数器用预定的颜色或渐变填充路径,xy可以更改为最佳外观。

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[x=2.5cm,y=2cm]
\coordinate (A) at (-3,-1) {};
\coordinate (B) at (3,-1) {};
\coordinate (C) at (0,5) {};
\foreach \A/\col [count=\i] in {Non-League/green, League Two/cyan,League One/yellow,Championship/blue,Premier\\League/orange}
\draw[fill=\col] (C)--([shift={(-.5*\i,1*\i)}]B)--node[above,align=center] {\A}([shift={(.5*\i,1*\i)}]A)--cycle; 
\end{tikzpicture}

\end{document}

在此处输入图片描述

或者像这样的渐变:

\foreach \A/\col [count=\i,evaluate=\i as \j using 10*\i] in {Non-League, League Two,League One,Championship,Premier\\League}
\draw[fill=red!\j] (C)--([shift={(-.5*\i,1*\i)}]B)--node[above,align=center] {\A}([shift={(.5*\i,1*\i)}]A)--cycle;

在此处输入图片描述

答案3

我来晚了。我刚刚和 Ti 完成了一个小项目Z 库,shapes.geometric所以我想在这里给出我的 2¢。这里没有什么非常复杂微妙的东西——只是简单的 TiZ. 代码中有注释可以提供帮助。请尝试regular polygon sides=456

\documentclass[]{article}

\usepackage[rgb]{xcolor} %% If you use \pyramidhue, you will need this; TikZ does not work with hsb
\usepackage{xparse}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric,positioning}

%% |=====8><-----| %%

\tikzset{tri/.style={%
        regular polygon,
        regular polygon sides=3, %% For fun, vary this number at will
        minimum size=#1,
        draw,
        thick,
        anchor=north
    },
    ptext/.style={font=\bfseries,align=center,text width=0.8*\pyrsize}
}

\NewDocumentCommand{\pyramid}{sO{}mm}{% #3 size; #4 entries
    \begin{tikzpicture}
        \pgfmathsetmacro{\incrrate}{0.75}% The rate at which the triangles decrease in size
        \coordinate (T) at (0,0);
        \foreach \test/\testi [count=\testnum from 1] in {#4}{\xdef\tot{\testnum}}%
        \pgfmathsetmacro{\incr}{#3/\tot}
        \foreach \step/\col [count=\stepnum from 0] in {#4}{%
            \pgfmathsetlengthmacro{\pyrsize}{#3-\incrrate*\stepnum*\incr}
            \node[tri=\pyrsize,fill=\col] (T\stepnum) at (T) {};
            \ifnum\stepnum=\numexpr\tot-1\relax
                \pgfmathsetlengthmacro{\lift}{0.0*\incr*\incrrate}% raise text in top shape
            \else
                \pgfmathsetlengthmacro{\lift}{0.1*\incr*\incrrate}% raise text in remaining shapes
            \fi
            \node[above=\lift of T\stepnum.south,ptext] {\step\strut};
        }%
    \end{tikzpicture}%
}

\NewDocumentCommand{\pyramidshade}{sO{}mmm}{% #3 size; #4 base shade; #5 entries
    \begin{tikzpicture}
        \pgfmathsetmacro{\incrrate}{0.75}% The rate at which the triangles decrease in size
        \coordinate (T) at (0,0);
        \foreach \test [count=\testnum from 1] in {#5}{\xdef\tot{\testnum}}%
        \pgfmathsetmacro{\incr}{#3/\tot}
        \foreach \step [count=\stepnum from 0] in {#5}{%
            \pgfmathsetlengthmacro{\pyrsize}{#3-\incrrate*\stepnum*\incr}
            \pgfmathsetmacro{\shade}{(\tot-\stepnum)/\tot*100}
            \node[tri=\pyrsize,fill=#4!\shade] (T\stepnum) at (T) {};
            \ifnum\stepnum=\numexpr\tot-1\relax
                \pgfmathsetlengthmacro{\lift}{0.1*\incr*\incrrate}% raise text in top shape
            \else
                \pgfmathsetlengthmacro{\lift}{0.1*\incr*\incrrate}% raise text in remaining shapes
            \fi
            \node[above=\lift of T\stepnum.south,ptext] {\step\strut};
        }%
    \end{tikzpicture}%
}

\NewDocumentCommand{\pyramidhue}{sO{}mm}{% #3 size; #4 entries
    \begin{tikzpicture}
        \pgfmathsetmacro{\incrrate}{0.75}% The rate at which the triangles decrease in size
        \coordinate (T) at (0,0);
        \foreach \test [count=\testnum from 1] in {#4}{\xdef\tot{\testnum}}%
        \pgfmathsetlengthmacro{\incr}{#3/\tot}
        \foreach \step [count=\stepnum from 0] in {#4}{%
            \pgfmathsetlengthmacro{\pyrsize}{#3-\incrrate*\stepnum*\incr}
            \pgfmathsetmacro{\myhue}{(\tot-\stepnum)/\tot}%
            \definecolor{mycolor}{hsb}{\myhue,0.5,1}% See xcolor docs v2.12, page 18 ff.
            \node[tri=\pyrsize,fill=mycolor] (T\stepnum) at (T) {};
            \ifnum\stepnum=\numexpr\tot-1\relax
                \pgfmathsetlengthmacro{\lift}{0.0*\incr*\incrrate}% raise text in top shape
            \else
                \pgfmathsetlengthmacro{\lift}{0.1*\incr*\incrrate}% raise text in remaining shapes
            \fi
            \node[above=\lift of T\stepnum.south,ptext] {\step\strut};
        }%
    \end{tikzpicture}%
}

%% |=====8><-----| %%

\begin{document}

\centering

\pyramid{3.0in}{Non-League/green,League Two/blue!50!white,League One/yellow,Championship/blue!70!white,Premier\\League/orange}

\bigskip

\pyramidshade{3.0in}{purple!70!red!50!white}{Non-League,League Two,League One,Championship,Premier\\League}

\bigskip

\pyramidhue{3.0in}{Non-League,League Two,League One,Championship,Premier\\League}


\end{document}

阴影金字塔

相关内容