1

I have the following graph and I was wondering how I could color each node with different colors.

Graph Visualization

\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}
7
  • You question is a little bit unclear. Why don´t you just write the color inside the optinal argument in the node? You colored them all white e.g. instead of fill=white write fill=red or am I missing something?
    – Roland
    Apr 26, 2021 at 8:42
  • I apologize for the bad wording. I’m trying to visualize my chromatic number problem using the graph above and I just can’t seem to figure out how to color individual nodes. I tried your take on writing fill=red but it won’t do me any good.
    – Minho Cho
    Apr 26, 2021 at 8:55
  • Do not just add fill=red before fill=white but replace fill=white with fill=red
    – Ivan
    Apr 26, 2021 at 9:26
  • Wow I can’t believe I totally missed that... thank you very much!
    – Minho Cho
    Apr 26, 2021 at 9:35
  • I clean your code \documentclass[border=2cm]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture}[scale=1.5, every node/.style={circle,draw,inner sep=2pt}] \path (-4,0) node (1) {$1$} (-3,-1) node (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$}
    – Black Mild
    Apr 26, 2021 at 11:13

1 Answer 1

3

Here is my suggestion.

  • \foreach should be used to shorten the code.
  • nodes={...} means that node options ... are added in the current scope. In this situation, the scope is inside the \path command; and other node options (like [fill=yellow]) can be freely added more.

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .