4
$\begingroup$

I want to visualize the difference between members of a set:

set = {20, 36, 70, 96, 152, 301}

This way I get the diffs:

diffs = Select[#[[2]] - #[[1]] & /@ Tuples[{set, set}], # > 0 &]

{16, 50, 76, 132, 281, 34, 60, 116, 265, 26, 82, 231, 56, 205, 149}

But how can I put the diffs as a caption over graph like lines between members?

$\endgroup$

2 Answers 2

10
$\begingroup$

Borrowing some code from Kuba's:

set = {20, 36, 70, 96, 152, 301}
Graph[DirectedEdge @@@ #,
   VertexShapeFunction -> "Square",
   VertexSize -> {.2, .1},
   VertexLabels -> Placed["Name", Center], 
   VertexLabelStyle -> Directive[FontFamily -> "Arial", 10], 
   GraphLayout -> "CircularEmbedding",
   EdgeLabels -> ((DirectedEdge[##] -> Placed[#2 - #, .85]) & @@@ #)] &@Subsets[set, {2}]

Mathematica graphics

Edit

In fact, you could apply any nice formatting to the edges. The following applies a color proportional to its value:

set = {20, 36, 70, 96, 152, 301};
{min, max} = {Min@#, Max@#} &[Subtract @@@ -Subsets[set, {2}]]
Graph[Property[DirectedEdge @@ #, 
               EdgeStyle -> {Thick, ColorData["Rainbow"][
                             Rescale[#[[2]] - #[[1]], {min, max}]]}] & /@ #,
   VertexShapeFunction -> "Square",
   VertexSize -> {.2, .1},
   VertexLabels -> Placed["Name", Center], 
   VertexLabelStyle -> Directive[FontFamily -> "Arial", 10], 
   GraphLayout -> "CircularEmbedding", 
   EdgeLabels -> ((DirectedEdge[##] -> Placed[#2 - #, .85]) & @@@ #)] 
 &@Subsets[set, {2}]

Mathematica graphics

$\endgroup$
5
$\begingroup$
set = {20, 36, 70, 96, 152, 301}

Graph[DirectedEdge @@@ #, EdgeLabels -> ((DirectedEdge[##] -> (#2 - #)) & @@@ #), 
                          VertexLabels -> Thread[set -> set], EdgeLabelStyle -> Bold, 
                          VertexLabelStyle -> Directive[Bold, 20]
     ] &[Tuples[set, {2}]]

enter image description here

 Graph[DirectedEdge @@@ #, EdgeLabels -> ((DirectedEdge[##] -> (#2 - #)) & @@@ #), 
                           VertexLabels -> Thread[set -> set], 
                           EdgeLabelStyle -> Directive[Bold, 15], 
                           GraphLayout -> "StarEmbedding", ImagePadding -> 35, 
                           VertexLabelStyle -> Directive[Bold, 20]
      ] &[Subsets[set, {2}]]

enter image description here

$\endgroup$
4
  • $\begingroup$ I just want one path between two nodes. How can I remove the negative ones? $\endgroup$ Commented Dec 20, 2013 at 12:43
  • 3
    $\begingroup$ @MohsenAfshin You could try Subsets[set, {2}] rather than Tuples... $\endgroup$
    – cormullion
    Commented Dec 20, 2013 at 12:54
  • $\begingroup$ I did, but the visual output is not as readable as what @Kuba rendered, how can I force the circular rendering? $\endgroup$ Commented Dec 20, 2013 at 13:02
  • $\begingroup$ @MohsenAfshin take a look at GraphLayout, e.g "CircularEmbedding" $\endgroup$
    – Kuba
    Commented Dec 20, 2013 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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