我有下面的图表,我想知道如何用不同的颜色为每个节点着色。
\documentclass[border=2cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1.5]
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -4, 0) (1){$1$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -3, -1) (2){$2$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -3, -2) (3){$3$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -2, -1) (4){$4$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -2, -2) (5){$5$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -1, -1) (6){$6$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -1, -2) (7){$7$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 0, -1) (8){$8$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 1, -1) (9){$9$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 2, -1) (10){$10$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 3, -1) (11){$11$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -2, 0) (12){$12$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -1, 2) (13){$13$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( -1, 1) (14){$14$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 0, 2) (15){$15$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 0, 1) (16){$16$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 0, 0) (17){$17$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 1, 4) (18){$18$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 1, 3) (19){$19$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 1, 2) (20){$20$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 1, 1) (21){$21$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 1, 0) (22){$22$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 2, 2) (23){$23$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 2, 1) (24){$24$};
\node[circle,draw=black, fill=white, inner sep=2pt,minimum size=3pt] at ( 2, 0) (25){$25$};
\draw[<->, thick] (1) -- (2) node[]{};
\draw[<->, thick] (2) -- (3) node[]{};
\draw[<->, thick] (2) -- (4) node[]{};
\draw[<->, thick] (3) -- (4) node[]{};
\draw[<->, thick] (3) -- (5) node[]{};
\draw[<->, thick] (4) -- (5) node[]{};
\draw[<->, thick] (4) -- (6) node[]{};
\draw[<->, thick] (5) -- (7) node[]{};
\draw[<->, thick] (6) -- (7) node[]{};
\draw[<->, thick] (6) -- (8) node[]{};
\draw[<->, thick] (7) -- (8) node[]{};
\draw[<->, thick] (8) -- (9) node[]{};
\draw[<->, thick] (9) -- (10) node[]{};
\draw[<->, thick] (10) -- (11) node[]{};
\draw[<->, thick] (12) -- (13) node[]{};
\draw[<->, thick] (12) -- (14) node[]{};
\draw[<->, thick] (12) -- (16) node[]{};
\draw[<->, thick] (12) -- (17) node[]{};
\draw[<->, thick] (13) -- (14) node[]{};
\draw[<->, thick] (13) -- (15) node[]{};
\draw[<->, thick] (14) -- (15) node[]{};
\draw[<->, thick] (14) -- (16) node[]{};
\draw[<->, thick] (15) -- (16) node[]{};
\draw[<->, thick] (15) -- (20) node[]{};
\draw[<->, thick] (15) -- (21) node[]{};
\draw[<->, thick] (16) -- (17) node[]{};
\draw[<->, thick] (16) -- (22) node[]{};
\draw[<->, thick] (17) -- (22) node[]{};
\draw[<->, thick] (18) -- (19) node[]{};
\draw[<->, thick] (18) -- (23) node[]{};
\draw[<->, thick] (19) -- (20) node[]{};
\draw[<->, thick] (19) -- (23) node[]{};
\draw[<->, thick] (20) -- (21) node[]{};
\draw[<->, thick] (20) -- (23) node[]{};
\draw[<->, thick] (20) -- (24) node[]{};
\draw[<->, thick] (21) -- (22) node[]{};
\draw[<->, thick] (21) -- (24) node[]{};
\draw[<->, thick] (21) -- (25) node[]{};
\draw[<->, thick] (22) -- (25) node[]{};
\draw[<->, thick] (23) -- (24) node[]{};
\draw[<->, thick] (24) -- (25) node[]{};
\end{tikzpicture}
\end{document}
答案1
这是我的建议。
\foreach
应该用来缩短代码。nodes={...}
表示...
在当前作用域内添加节点选项,此时作用域在命令内部\path
;其他节点选项(如[fill=yellow]
)可以自由添加。
\documentclass[border=5mm]{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{center}
\begin{tikzpicture}[scale=1.2]
\path[nodes={circle,draw,minimum size=8mm}]
(-4,0) node (1) {$1$}
(-3,-1) node[fill=yellow] (2) {$2$}
(-3,-2) node (3) {$3$}
(-2,-1) node (4) {$4$}
(-2,-2) node (5) {$5$}
(-1,-1) node (6) {$6$}
(-1,-2) node (7) {$7$}
(0,-1) node (8) {$8$}
(1,-1) node (9) {$9$}
(2,-1) node (10){$10$}
(3,-1) node (11){$11$}
(-2,0) node (12){$12$}
(-1,2) node (13){$13$}
(-1,1) node (14){$14$}
(0,2) node (15){$15$}
(0,1) node[fill=cyan] (16){$16$}
(0,0) node (17){$17$}
(1,4) node (18){$18$}
(1,3) node (19){$19$}
(1,2) node (20){$20$}
(1,1) node (21){$21$}
(1,0) node (22){$22$}
(2,2) node[fill=pink] (23){$23$}
(2,1) node (24){$24$}
(2,0) node (25){$25$};
\foreach \i/\j in {1/2,2/3,2/4,3/4,3/5,4/5,4/6,5/7,6/7,6/8,7/8,8/9,9/10,10/11,12/13,12/14,12/16,12/17,13/14,13/15,14/15,14/16,15/16,15/20,15/21,16/17,16/22,17/22,18/19,18/23,19/20,19/23,20/21,20/23,20/24,21/22,21/24,21/25,22/25,23/24,24/25}
\draw[<->,thick] (\i)--(\j);
\end{tikzpicture}
\end{center}
\lipsum[2]
\end{document}