TikZ 피라미드 계층 색상

TikZ 피라미드 계층 색상

나에겐 티가 있다케이Z 피라미드 계층 구조, 그러나 다양한 계층을 색상으로 채우는 방법이 있는지 궁금합니다. 내 현재 코드는 다음과 같습니다

\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의 마지막 요소를 추가로 채웁니다.

첫 번째 방법은 배열의 색상 목록을 포함합니다. countforeach에 를 추가 하고 이를 사용하여 색상을 선택합니다. 배열에는 자동 숫자 인덱스가 있으므로 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간단한 삼각형 경로를 사용하여 아래 루프의 카운터 함수에 따라 미리 결정된 색상이나 그라데이션으로 경로를 채울 수 있습니다 . 최상의 외관을 위해 변경될 수 있습니다 x.y

\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센트를 기부할 것이라고 생각했습니다. 여기에는 매우 복잡하고 미묘한 것이 없습니다. 그냥 간단합니다.케이Z. 코드에 도움이 되는 주석이 있습니다. regular polygon sides=4또는 5또는 을 시도해 보세요 6.

\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}

그늘의 피라미드

관련 정보