问题
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',
1876: 'darkturquoise',
1877: 'skyblue',
5974: 'gray',
3932: 'olive',
1887: 'gainsboro',
3936: 'lawngreen',
1889: 'silver',
8043: 'silver',
1904: 'darkorange',
1921: 'lightslategray',
6020: 'silver',
1932: 'lightslategray',
6030: 'beige',
1936: 'darkturquoise',
6035: 'blueviolet',
8084: 'crimson',
10140: 'lightslategray',
8092: 'mediumaquamarine',
8095: 'ghostwhite',
8099: 'darkslategrey',
6057: 'darkslategrey',
6058: 'slateblue',
6066: 'darkslategrey',
8118: 'royalblue',
6071: 'mediumaquamarine',
4022: 'lavenderblush',
6073: 'blueviolet',
6074: 'silver',
1978: 'olive',
8122: 'lawngreen',
4026: 'darkorange',
1986: 'olivedrab',
2003: 'darkorange',
2005: 'lavenderblush',
6105: 'crimson',
6108: 'gray',
8157: 'lawngreen',
6129: 'navajowhite',
2033: 'mediumaquamarine',
10225: 'darkslategrey',
6141: 'lawngreen'}
which has the following edges (keys from above dictionary):
edges = [(6, 176), (6, 283), (6, 394), (6, 446), (6, 645), (6, 996), (6, 1811), (6, 1876), (6, 2521), (6, 2999), (6, 3122), (6, 3198), (6, 3262), (6, 3339), (6, 3407), (6, 3608), (6, 4374), (6, 4491), (6, 4652), (6, 4723), (6, 4843), (6, 4984), (6, 4997), (6, 5092), (6, 5152), (6, 5259), (6, 5685), (6, 5705), (6, 5906), (6, 6066), (6, 6141), (6, 6551), (6, 6832), (6, 6907), (6, 6959), (6, 7098), (6, 7149), (6, 7228), (6, 7806), (6, 7922), (6, 7989), (6, 8099), (6, 8234), (6, 8387), (6, 8525), (6, 9824), (6, 9919), (6, 9997), (6, 2241), (6, 6873), (6, 8868), (6, 8911), (6, 533), (6, 1681), (6, 2327), (6, 2786), (6, 3453), (6, 3526), (6, 4211), (6, 4404), (6, 4669), (6, 5142), (6, 5681), (6, 8476), (6, 9480), (6, 3114), (6, 1327), (6, 2005), (6, 2205), (6, 3785), (6, 4851), (6, 5068), (6, 5402), (6, 5625), (6, 6373), (6, 6917), (6, 9188), (6, 10), (6, 36), (6, 534), (6, 687), (6, 746), (6, 755), (6, 1160), (6, 1172), (6, 1284), (6, 1511), (6, 1645), (6, 1731), (6, 1887), (6, 1978), (6, 2085), (6, 2150), (6, 2188), (6, 2313), (6, 2628), (6, 2633), (6, 2638), (6, 2665), (6, 2750), (6, 2977), (6, 3056), (6, 3320), (6, 3334), (6, 3500), (6, 3509), (6, 3538), (6, 3690), (6, 3765), (6, 3783), (6, 3858), (6, 4231), (6, 4256), (6, 4496), (6, 4507), (6, 4566), (6, 4648), (6, 4708), (6, 4774), (6, 4908), (6, 4931), (6, 5030), (6, 5466), (6, 5546), (6, 5673), (6, 5700), (6, 6035), (6, 6317), (6, 6332), (6, 6395), (6, 6475), (6, 6566), (6, 6660), (6, 6749), (6, 7112), (6, 7132), (6, 7234), (6, 7305), (6, 7394), (6, 7533), (6, 7625), (6, 7633), (6, 7714), (6, 8084), (6, 8259), (6, 8285), (6, 8310), (6, 8888), (6, 8924), (6, 9172), (6, 9358), (6, 9481), (6, 9548), (6, 9607), (6, 9844), (6, 9890), (6, 10013), (6, 10045), (6, 10140), (6, 10225), (6, 10271), (10, 176), (10, 283), (10, 394), (10, 446), (10, 645), (10, 792), (10, 2521), (10, 3122), (10, 3198), (10, 3339), (10, 3407), (10, 3561), (10, 3820), (10, 4374), (10, 4652), (10, 4723), (10, 4843), (10, 4984), (10, 4997), (10, 5152), (10, 5259), (10, 6639), (10, 6753), (10, 6832), (10, 6959), (10, 7098), (10, 7228), (10, 7373), (10, 7966), (10, 7989), (10, 8099), (10, 8859), (10, 9919), (10, 9997), (10, 6873), (10, 8868), (10, 8911), (10, 1009), (10, 3323), (10, 3526), (10, 5142), (10, 8270), (10, 8476), (10, 449), (10, 1226), (10, 1452), (10, 3785), (10, 4407), (10, 6073), (10, 6371), (10, 6373), (10, 6917), (10, 7417), (10, 9105), (10, 36), (10, 128), (10, 395), (10, 399), (10, 534), (10, 755), (10, 858), (10, 902), (10, 1165), (10, 1273), (10, 1391), (10, 1511), (10, 1742), (10, 1932), (10, 2210), (10, 2599), (10, 2638), (10, 2796), (10, 3056), (10, 3320), (10, 3386), (10, 3607), (10, 3661), (10, 3713), (10, 3764), (10, 3775), (10, 4022), (10, 4109), (10, 4231), (10, 4256), (10, 4480), (10, 4622), (10, 4708), (10, 5051), (10, 5199), (10, 5607), (10, 5686), (10, 6035), (10, 6071), (10, 6105), (10, 6129), (10, 6318), (10, 6435), (10, 6566), (10, 6614), (10, 6728), (10, 6748), (10, 6794), (10, 6986), (10, 7010), (10, 7129), (10, 7224), (10, 7305), (10, 7376), (10, 7533), (10, 7625), (10, 7648), (10, 7720), (10, 7730), (10, 8092), (10, 8122), (10, 8259), (10, 8471), (10, 8561), (10, 8736), (10, 8768), (10, 8775), (10, 8888), (10, 9098), (10, 9358), (10, 9653), (10, 9674), (10, 9844), (10, 9905), (10, 10271), (4109, 176), (4109, 233), (4109, 283), (4109, 394), (4109, 446), (4109, 1443), (4109, 2521), (4109, 2999), (4109, 3122), (4109, 3198), (4109, 3262), (4109, 3339), (4109, 3407), (4109, 3561), (4109, 3608), (4109, 4374), (4109, 4390), (4109, 4491), (4109, 4652), (4109, 4836), (4109, 4997), (4109, 5092), (4109, 5152), (4109, 5241), (4109, 5259), (4109, 5450), (4109, 5705), (4109, 5906), (4109, 6832), (4109, 6959), (4109, 7373), (4109, 7989), (4109, 8234), (4109, 8525), (4109, 8859), (4109, 9919), (4109, 9997), (4109, 2241), (4109, 6873), (4109, 8911), (4109, 533), (4109, 739), (4109, 1009), (4109, 2327), (4109, 2358), (4109, 2443), (4109, 2786), (4109, 3323), (4109, 3526), (4109, 3560), (4109, 4404), (4109, 4669), (4109, 5142), (4109, 8270), (4109, 8476), (4109, 3114), (4109, 449), (4109, 1226), (4109, 1327), (4109, 3785), (4109, 4187), (4109, 4490), (4109, 4561), (4109, 5068), (4109, 5402), (4109, 5625), (4109, 6073), (4109, 6373), (4109, 8157), (4109, 9105), (4109, 9188), (4109, 534), (4109, 1284), (4109, 1731), (4109, 1887), (4109, 2188), (4109, 2638), (4109, 2665)]
If I try to draw the graph normally, I get random positions:
RRR = nx.Graph()
RRR.add_edges_from(edges)
nx.draw(RRR)
Is there a way to draw this graph with nodes grouped based on the values from the nodesWithGroup dictionary. I have tried to apply the methods described in this question, but I couldn't figure out how to apply the logic.
回答1:
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:
来源:https://stackoverflow.com/questions/60061960/draw-nodes-in-a-graph-clustered-based-on-color