我陷入以下困境:
如何使用各种颜色为 tikz 中的图形节点着色?
我写了以下代码:
\begin{figure}
\centering
\begin{tikzpicture}
\node[shape=circle,draw=black] (0) at (0,0) {$0$};
\node[shape=circle,draw=black] (4) at (6,0) {$4$};
\node[shape=circle,draw=black] (1) at (3,-1) {$1$};
\node[shape=circle,draw=black] (2) at (3,-2) {$2$};
\node[shape=circle,draw=black] (3) at (3,-3) {$3$};
\node[shape=circle,draw=black] (5) at (3,1) {$5$};
\node[shape=circle,draw=black] (6) at (3,2) {$6$};
\node[shape=circle,draw=black] (7) at (3,3) {$7$};
\draw (0) -- (1);
\draw (0) -- (2);
\draw (0) -- (3);
\draw (0) -- (4);
\draw (0) -- (5);
\draw (0) -- (6);
\draw (0) -- (7);
\draw (4) -- (1);
\draw (4) -- (2);
\draw (4) -- (3);
\draw (4) -- (5);
\draw (4) -- (6);
\draw (4) -- (7);
\end{tikzpicture}
\end{figure}
我想将节点涂成红色、黄色等。我还想将边涂成红色、黄色等。但我不知道如何用不同的颜色为一个图中的顶点和另一个图中的边涂上颜色。
笔记:请告诉我如何修改现有代码以将颜色添加到顶点。请不要建议使用完全不同的代码。
有人可以帮帮我吗?
答案1
看看这个:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\node[shape=circle,draw=black,fill=red] (0) at (0,0) {$0$};
\node[shape=circle,draw=black,fill=yellow] (4) at (6,0) {$4$};
\node[shape=circle,draw=black] (1) at (3,-1) {$1$};
\node[shape=circle,draw=black] (2) at (3,-2) {$2$};
\node[shape=circle,draw=black] (3) at (3,-3) {$3$};
\node[shape=circle,draw=black] (5) at (3,1) {$5$};
\node[shape=circle,draw=black] (6) at (3,2) {$6$};
\node[shape=circle,draw=black] (7) at (3,3) {$7$};
\draw[color=red] (0) -- (1);
\draw[color=yellow] (0) -- (2);
\draw[color=blue] (0) -- (3);
\draw[color=purple] (0) -- (4);
\draw[color=brown] (0) -- (5);
\draw[color=pink] (0) -- (6);
\draw[color=green] (0) -- (7);
\draw[color=purple] (4) -- (1);
\draw[color=brown] (4) -- (2);
\draw[color=pink] (4) -- (3);
\draw[color=green] (4) -- (5);
\draw[color=red] (4) -- (6);
\draw[color=yellow](4) -- (7);
\end{tikzpicture}
\end{figure}
\end{document}
由此产生了以下主和平: