Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

python - Draw nodes in a graph clustered based on color

In the following dictionary mapping nodes to color, I wanted to draw the resultant graph whilst clustering the nodes within the graph based on their color. That is if node 4187 and 8285 have crimson color, I want them to appear next to each other on the graph.

My dictionary is as follows:

nodesWithGroup= {6: 'slateblue',
     10: 'skyblue',
     4109: 'lawngreen',
     2062: 'mediumaquamarine',
     28: 'olive',
     10269: 'crimson',
     6175: 'aqua',
     10271: 'crimson',
     36: 'mediumaquamarine',
     2085: 'darkturquoise',
     2084: 'navajowhite',
     8234: 'crimson',
     6187: 'silver',
     2092: 'navajowhite',
     10297: 'mediumaquamarine',
     4154: 'silver',
     8259: 'gray',
     8270: 'olive',
     88: 'sandybrown',
     4187: 'crimson',
     8285: 'crimson',
     6243: 'lightsalmon',
     2150: 'beige',
     2156: 'silver',
     8302: 'lavenderblush',
     4211: 'royalblue',
     8310: 'silver',
     4219: 'limegreen',
     124: 'darkorange',
     128: 'gray',
     129: 'lightsalmon',
     4231: 'darkturquoise',
     2188: 'navajowhite',
     2205: 'crimson',
     4256: 'lawngreen',
     161: 'blueviolet',
     2210: 'gray',
     4264: 'lawngreen',
     4265: 'darkgoldenrod',
     6317: 'skyblue',
     6318: 'crimson',
     8367: 'limegreen',
     176: 'crimson',
     2228: 'lightsalmon',
     6332: 'thistle',
     4288: 'antiquewhite',
     2241: 'gray',
     8387: 'crimson',
     8396: 'skyblue',
     219: 'gray',
     6371: 'silver',
     6373: 'darkgoldenrod',
     229: 'sandybrown',
     233: 'silver',
     4329: 'darkgoldenrod',
     8441: 'brown',
     6395: 'lightsteelblue',
     4360: 'olive',
     8457: 'mediumaquamarine',
     2313: 'darkturquoise',
     269: 'royalblue',
     4365: 'limegreen',
     4374: 'lawngreen',
     2327: 'silver',
     8471: 'darkgoldenrod',
     283: 'mediumslateblue',
     8476: 'darkgoldenrod',
     288: 'skyblue',
     2338: 'mediumslateblue',
     6435: 'olive',
     2340: 'darkslategrey',
     4390: 'ghostwhite',
     295: 'mediumslateblue',
     2345: 'darkorange',
     8498: 'darkorange',
     4404: 'darkgoldenrod',
     2358: 'skyblue',
     4407: 'skyblue',
     315: 'gray',
     4415: 'silver',
     2369: 'darkslategrey',
     6475: 'blueviolet',
     8525: 'mediumaquamarine',
     4433: 'skyblue',
     4436: 'gray',
     2405: 'ghostwhite',
     8561: 'darkturquoise',
     377: 'olive',
     4480: 'skyblue',
     393: 'silver',
     394: 'mediumaquamarine',
     2443: 'darkturquoise',
     4490: 'ghostwhite',
     4491: 'blueviolet',
     395: 'lawngreen',
     399: 'lawngreen',
     4496: 'thistle',
     4497: 'gray',
     6547: 'slateblue',
     2453: 'darkgoldenrod',
     4501: 'lightsalmon',
     6551: 'darkgoldenrod',
     4507: 'limegreen',
     6566: 'skyblue',
     8615: 'skyblue',
     446: 'gray',
     449: 'blueviolet',
     4561: 'darkolivegreen',
     4566: 'royalblue',
     6614: 'darkslategrey',
     2521: 'skyblue',
     4573: 'mediumslateblue',
     4584: 'gray',
     6639: 'darkgoldenrod',
     501: 'silver',
     4599: 'darkslategrey',
     6660: 'gray',
     4622: 'blueviolet',
     533: 'sandybrown',
     534: 'olivedrab',
     8730: 'darkslategrey',
     6686: 'lawngreen',
     8736: 'darkslategrey',
     2597: 'crimson',
     2599: 'sandybrown',
     4648: 'gray',
     4652: 'silver',
     556: 'gray',
     2606: 'silver',
     4667: 'mediumaquamarine',
     4669: 'lightslategrey',
     2622: 'darkslategrey',
     2624: 'darkolivegreen',
     8768: 'sandybrown',
     2625: 'darkorange',
     4672: 'darkgoldenrod',
     2628: 'skyblue',
     4677: 'beige',
     8775: 'gray',
     6728: 'royalblue',
     2633: 'olive',
     587: 'darkturquoise',
     2638: 'darkslategrey',
     590: 'darkgoldenrod',
     2651: 'darkorange',
     6748: 'blueviolet',
     6749: 'darkslategrey',
     6753: 'darkolivegreen',
     4708: 'antiquewhite',
     615: 'olive',
     2665: 'olive',
     8810: 'lawngreen',
     6764: 'mediumaquamarine',
     6769: 'darkolivegreen',
     4723: 'olivedrab',
     4733: 'darkturquoise',
     645: 'darkslategrey',
     647: 'gray',
     2696: 'skyblue',
     6794: 'gray',
     2698: 'gray',
     8859: 'mediumaquamarine',
     667: 'darkturquoise',
     6813: 'lightsteelblue',
     8868: 'gray',
     4774: 'antiquewhite',
     2733: 'darkorange',
     687: 'darkgoldenrod',
     6832: 'darkslategrey',
     8888: 'gray',
     2750: 'silver',
     4805: 'lightsalmon',
     8911: 'darkslategrey',
     2771: 'olivedrab',
     4820: 'ghostwhite',
     2775: 'ghostwhite',
     6873: 'sandybrown',
     8924: 'darkolivegreen',
     6881: 'darkslategrey',
     2786: 'silver',
     739: 'darkslategrey',
     4836: 'mediumaquamarine',
     6886: 'blueviolet',
     746: 'silver',
     4843: 'crimson',
     2796: 'thistle',
     750: 'lightslategray',
     2800: 'beige',
     4849: 'olive',
     2803: 'silver',
     4851: 'limegreen',
     755: 'silver',
     6907: 'darkgoldenrod',
     6911: 'blueviolet',
     6915: 'mediumaquamarine',
     6917: 'olive',
     4870: 'ghostwhite',
     8967: 'gray',
     2830: 'silver',
     2832: 'navajowhite',
     2834: 'skyblue',
     8979: 'darkgoldenrod',
     792: 'darkturquoise',
     6943: 'ghostwhite',
     6950: 'ghostwhite',
     4908: 'sandybrown',
     2861: 'silver',
     6959: 'skyblue',
     828: 'skyblue',
     4928: 'olivedrab',
     6977: 'mediumaquamarine',
     4931: 'darkgoldenrod',
     4932: 'darkslategrey',
     6986: 'skyblue',
     858: 'darkgoldenrod',
     7010: 'limegreen',
     867: 'ghostwhite',
     4964: 'gray',
     878: 'thistle',
     2928: 'skyblue',
     9077: 'mediumaquamarine',
     4984: 'mediumaquamarine',
     9092: 'blueviolet',
     4997: 'navajowhite',
     902: 'silver',
     9098: 'skyblue',
     2955: 'skyblue',
     911: 'gray',
     9105: 'skyblue',
     2964: 'mediumaquamarine',
     7061: 'lawngreen',
     2967: 'lawngreen',
     2977: 'skyblue',
     7076: 'lawngreen',
     5030: 'silver',
     2999: 'limegreen',
     7098: 'mediumaquamarine',
     5051: 'darkturquoise',
     9153: 'darkturquoise',
     964: 'gray',
     7112: 'blueviolet',
     969: 'darkturquoise',
     970: 'olive',
     5068: 'skyblue',
     9172: 'sandybrown',
     7129: 'gray',
     7132: 'mediumaquamarine',
     991: 'ghostwhite',
     9188: 'skyblue',
     996: 'navajowhite',
     5092: 'skyblue',
     998: 'mediumslateblue',
     1001: 'ghostwhite',
     7149: 'gray',
     3056: 'mediumaquamarine',
     1009: 'mediumaquamarine',
     9203: 'gainsboro',
     9216: 'olive',
     5122: 'darkslategrey',
     5142: 'lightslategrey',
     1047: 'darkslategrey',
     7196: 'ghostwhite',
     5152: 'darkorange',
     3114: 'lawngreen',
     3122: 'sandybrown',
     1075: 'ghostwhite',
     5172: 'darkslategrey',
     7224: 'ghostwhite',
     7228: 'lightslategray',
     7234: 'darkslategrey',
     3147: 'limegreen',
     5199: 'beige',
     1105: 'silver',
     3155: 'ghostwhite',
     5204: 'ghostwhite',
     1110: 'silver',
     3165: 'sandybrown',
     7262: 'ghostwhite',
     9311: 'crimson',
     1136: 'silver',
     5241: 'darkturquoise',
     7293: 'lawngreen',
     3198: 'royalblue',
     5245: 'gray',
     5256: 'mediumaquamarine',
     7305: 'mediumaquamarine',
     1160: 'ghostwhite',
     5259: 'gray',
     1165: 'darkgoldenrod',
     9358: 'lightslategrey',
     1172: 'darkslategrey',
     3222: 'darkturquoise',
     3224: 'mediumaquamarine',
     5272: 'royalblue',
     3240: 'skyblue',
     1196: 'ghostwhite',
     5296: 'skyblue',
     1206: 'slateblue',
     3258: 'crimson',
     3259: 'limegreen',
     3262: 'sandybrown',
     3266: 'darkslategrey',
     3269: 'silver',
     1226: 'ghostwhite',
     5323: 'ghostwhite',
     9420: 'ghostwhite',
     7373: 'blueviolet',
     7376: 'darkgoldenrod',
     3296: 'navajowhite',
     7393: 'mediumaquamarine',
     7394: 'mediumaquamarine',
     9448: 'darkorange',
     3320: 'royalblue',
     7417: 'antiquewhite',
     1273: 'sandybrown',
     3323: 'antiquewhite',
     9465: 'blueviolet',
     5377: 'lightslategrey',
     1283: 'royalblue',
     1284: 'silver',
     3334: 'silver',
     9479: 'gray',
     9480: 'blueviolet',
     9481: 'gray',
     3339: 'silver',
     1291: 'limegreen',
     5393: 'skyblue',
     5402: 'skyblue',
     3366: 'crimson',
     9517: 'gray',
     1327: 'silver',
     5426: 'lavenderblush',
     5429: 'gray',
     9526: 'darkorange',
     3386: 'lightslategray',
     1347: 'darkorange',
     3397: 'thistle',
     5450: 'skyblue',
     9548: 'lightsteelblue',
     1357: 'darkslategrey',
     3407: 'gray',
     3417: 'silver',
     5466: 'limegreen',
     7515: 'navajowhite',
     3425: 'lawngreen',
     5479: 'darkgoldenrod',
     1386: 'ghostwhite',
     7533: 'royalblue',
     1391: 'ghostwhite',
     3453: 'darkslategrey',
     1408: 'lightslategray',
     1413: 'lightslategray',
     9607: 'blueviolet',
     1417: 'silver',
     7565: 'silver',
     3489: 'thistle',
     3490: 'silver',
     1443: 'darkslategrey',
     5539: 'lawngreen',
     5544: 'navajowhite',
     5546: 'gray',
     3498: 'lavenderblush',
     3500: 'olive',
     1452: 'darkorange',
     5556: 'gray',
     9653: 'mediumaquamarine',
     3509: 'darkgoldenrod',
     1464: 'crimson',
     1471: 'lawngreen',
     3526: 'darkgoldenrod',
     7625: 'gainsboro',
     9674: 'darkorange',
     7633: 'sandybrown',
     3538: 'silver',
     7648: 'navajowhite',
     1511: 'darkolivegreen',
     3560: 'crimson',
     3561: 'sandybrown',
     1512: 'gray',
     5607: 'blueviolet',
     7663: 'lightslategray',
     3569: 'darkturquoise',
     5625: 'skyblue',
     7702: 'mediumaquamarine',
     3607: 'darkgoldenrod',
     3608: 'thistle',
     7714: 'skyblue',
     1572: 'lightsalmon',
     7720: 'darkgoldenrod',
     5673: 'silver',
     3632: 'sandybrown',
     5681: 'limegreen',
     7730: 'lightsteelblue',
     5685: 'gray',
     5686: 'gray',
     5688: 'lavenderblush',
     3643: 'skyblue',
     3650: 'ghostwhite',
     5700: 'gray',
     5705: 'lightsteelblue',
     3658: 'ghostwhite',
     3661: 'skyblue',
     9824: 'brown',
     3690: 'darkslategrey',
     1644: 'lawngreen',
     1645: 'darkturquoise',
     5747: 'skyblue',
     9844: 'sandybrown',
     3702: 'silver',
     3704: 'darkslategrey',
     7806: 'mediumaquamarine',
     3713: 'darkslategrey',
     1665: 'ghostwhite',
     5768: 'darkolivegreen',
     1681: 'skyblue',
     5784: 'ghostwhite',
     9890: 'beige',
     1705: 'ghostwhite',
     7851: 'olive',
     9905: 'antiquewhite',
     3764: 'lavender',
     3765: 'silver',
     3770: 'olive',
     1725: 'navajowhite',
     9919: 'ghostwhite',
     3775: 'crimson',
     1731: 'olive',
     3783: 'silver',
     3785: 'crimson',
     1740: 'darkorange',
     1742: 'lightsalmon',
     3797: 'antiquewhite',
     7895: 'silver',
     3806: 'gray',
     7908: 'royalblue',
     3820: 'silver',
     3823: 'darkorange',
     7922: 'gray',
     3830: 'olivedrab',
     5886: 'ghostwhite',
     3841: 'ghostwhite',
     9997: 'silver',
     3858: 'darkgoldenrod',
     5906: 'gray',
     1811: 'lightslategray',
     10013: 'mediumaquamarine',
     1822: 'lightslategray',
     7966: 'darkturquoise',
     1824: 'ghostwhite',
     5924: 'gray',
     5927: 'limegreen',
     3886: 'antiquewhite',
     7989: 'mediumaquamarine',
     3900: 'antiquewhite',
     10045: 'crimson',
     1857: 'lawngreen',
     3908: 'lavender',
     10063: 'gainsboro'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can be achieved by slightly adapting the logic in the linked question. Note that I have altered the radius value for the center circle, as the graph has so many nodes and edges, as well as increasing the size of the figure. In addition, I have altered the logic to accept the dictionary input in the question. When plotting the nodes, I have added a black border to the nodes with edgecolors=[(0,0,0,1)] as some nodes are white or off-white which made them hard to see.

Code:

import networkx
import numpy as np
import matplotlib.pyplot as plt

# Set up graph, adding nodes and edges
RRR = nx.Graph()
RRR.add_nodes_from(nodesWithGroup.keys())
RRR.add_edges_from(edges)

# Create a dictionary mapping color to a list of nodes
nodes_by_color = {}
for k, v in nodesWithGroup.items():
    if v not in nodes_by_color:
        nodes_by_color[v] = [k]
    else:
        nodes_by_color[v].append(k)

# Create initial circular layout
pos = nx.circular_layout(RRR)

# Get list of colors
colors = list(nodes_by_color.keys())

# OPTIONAL: shuffle colors list to get a random arrangement of colors
# import random
# random.shuffle(colors)

# Apply logic from linked answer: https://stackoverflow.com/a/55764135/12366110
# Note altered radius value. In addition, we get the value of posx from the colors list.
angs = np.linspace(0, 2*np.pi, 1+len(colors))
repos = []
rad = 13
for ea in angs:
    if ea > 0:
        repos.append(np.array([rad*np.cos(ea), rad*np.sin(ea)]))

for color, nodes in nodes_by_color.items():
    posx = colors.index(color)
    for node in nodes:
        pos[node] += repos[posx]


# Plot graph
plt.figure(figsize=(20,20))

for color, nodes in nodes_by_color.items():
    nx.draw_networkx_nodes(RRR, pos=pos, nodelist=nodes, node_color=color, edgecolors=[(0,0,0,1)])

nx.draw_networkx_edges(RRR, pos=pos)
plt.show()

Output:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...