I have a graph with 12 nodes. The x and y coordinates are give as

xcord={154.780094065851,    302.747331652902,   348.235746943268,   959.228504576796,   1021.26721027296,   1114.13365936238,   358.208241883160,   181.741156296042,   838.759299985938,   906.502702816486,   662.579285251179,   536.372745335719};

ycord={347.543166449207,    461.536311618993,   261.753095520236,   431.778284592051,   250.920713280060,   432.113192132818,   1044.66583584371,   1044.70069210915,   1135.19457237168,   984.121301679063,   792.153882418914,   708.946190878097};

Also, these are the edges connecting the nodes.


Let's say, The weight of the edges are randomly generated.

I want to plot the graph. The nodes should be depicted as circles with the index within it. I also want to place the weight over the edges.

How can I do it?

xy = Transpose[{xcord, ycord}];

ew = Round[ RandomReal[1, Length@edges], .01];

wam = SparseArray[Thread[edges -> ew], {12, 12}, Infinity];

 VertexCoordinates -> xy, 
 PlotTheme -> "IndexLabeled", 
 EdgeLabels -> "EdgeWeight", 
 ImageSize -> Large, 
 EdgeShapeFunction -> "CurvedEdge"] 

enter image description here

Update: Add the option

VertexStyle -> Directive[EdgeForm[{Thick, Blue}], LightBlue]

to color the vertices, and the option

EdgeStyle -> Thread[(DirectedEdge @@@ edges) -> 
  Thread[{Thick, Arrowheads[{-Large, Large}], ColorData["Rainbow"] /@ ew}]]

to style edges and add arrow heads at both ends of edges

enter image description here

To render the edges without arrow heads, either (1) use EdgeShapeFunction -> "CurvedArc" or (2) add the option DirectedEdges -> False and replace (DirectedEdge @@@ edges) with (UndirectedEdge @@@ edges) in EdgeStyle -> ...:

enter image description here

  • $\begingroup$ thanks a lot for your answer. The edges are not directional. we can have either (1) edges with no arrow at both ends or (2) arrows at both ends. Would you please plot both of this: $\endgroup$ Jun 7 at 23:28
  • $\begingroup$ Also, I would prefer colourful plot. nodes with same colour. The edges can take different colours. $\endgroup$ Jun 7 at 23:31
  • $\begingroup$ @dipaknarayanan, please see the update. $\endgroup$
    – kglr
    Jun 7 at 23:51
  • $\begingroup$ Thanks a lot. This is exactly what I was hoping. $\endgroup$ Jun 8 at 0:08
  • $\begingroup$ the EdgeStyle is nor working! Braket error! $\endgroup$ Jun 8 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.