2
$\begingroup$

I am trying to visualize some basic multi-graphs. However, the visualization is not giving correct results. I am not sure if it is a bug or I am doing something wrong.

edgeList = {UndirectedEdge[1, 2], UndirectedEdge[1, 4], 
  UndirectedEdge[1, 6], UndirectedEdge[1, 8], UndirectedEdge[2, 4], 
  UndirectedEdge[2, 5], UndirectedEdge[2, 7], UndirectedEdge[3, 4], 
  UndirectedEdge[3, 6], 
     UndirectedEdge[3, 7], UndirectedEdge[4, 5], 
  UndirectedEdge[4, 7], UndirectedEdge[4, 8], UndirectedEdge[5, 7], 
  UndirectedEdge[6, 8], DirectedEdge[1, 2]}     


styles = {Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Arrowheads[0.03`], Dashing[{Small, Small}], RGBColor[
   1, 0, 0]]}

i = 1;
Graph[edgeList, VertexLabels -> Automatic, 
 EdgeShapeFunction -> ({styles[[i++]], Arrow@#} &)]

enter image description here

The following minimal code works slightly better, though it gives an arrowhead for an undirercted edge. I could bypass that by specifying Arrowheads[0.0]. Another small note, I am not able to save any graph correctly. It does not give the same result when I do "Save Graphics As..".

edgeList1 = {UndirectedEdge[1, 2], DirectedEdge[1, 2]}
styles1 = {Directive[Thickness[Large], RGBColor[0, 0, 1]], 
  Directive[Arrowheads[0.03`], Dashing[{Small, Small}], RGBColor[
   1, 0, 0]]}

i = 1;

Graph[edgeList1, VertexLabels -> Automatic, 
 EdgeShapeFunction -> ({styles1[[i++]], Arrow@#} &)]

System details:

  1. Mathematica 11.2
  2. Ubuntu 16.04
$\endgroup$
6
$\begingroup$

The order in which styles are applied is not necessarily the same as the order in which the edges are given. It would be much better to rely on the second argument of EdgeShapeFunction to control which styles are applied. Here is a version that does this:

styleAssociation = AssociationThread[edgeList, styles];
Graph[
    edgeList,
    VertexLabels->Automatic,
    EdgeShapeFunction->({styleAssociation[Sort@#2],Arrow@#}&)
]

enter image description here

Update

Updated to add support for switching between Line/Arrow for the primitive. Here is the revised association:

assoc = AssociationThread[
    edgeList,
    Replace[
        Thread[{edgeList, styles}],
        {e_, s_} :> Association["directive" -> s, "primitive" -> If[MatchQ[e, _UndirectedEdge], Line, Arrow]],
        {1}
    ]
];

And, here is the corresponding Graph call:

Graph[
    edgeList,
    VertexLabels->Automatic,
    EdgeShapeFunction->({assoc[Sort@#2, "directive"], assoc[Sort@#2, "primitive"][#1]}&)
]

enter image description here

$\endgroup$
  • $\begingroup$ That works! Any idea why the undirected edges have arrow heads? They don't have arrow heads when there are only undirected edges. $\endgroup$ – Saurav Oct 30 '17 at 6:28

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.