2
$\begingroup$

I am trying to make a graph with some loops such as this one,

GraphPlot[{{2 -> -2, "a"}, {-2 -> 2, "b"}, {2 -> 6, "b"}, {6 -> 8, "b"}, {8 -> 10, "b"}, {10 -> 8, "c"}}]

Where the output is Mathematica Output

In my GraphPlot function, I have 2 -> -2 labeled as "a" and -2 -> 2 as "b". Instead, Mathematica chooses the first instance and gives the other leg in the loop the same label. So for example, if I were to switch the ordering {2 -> -2,"a"} and {-2 -> 2,"b"} that loop would be labeled as "b,b" instead of "a,a". Any suggestions on how to make the loop labeled as "a,b" would be much appreciated!

$\endgroup$
5
  • $\begingroup$ if you have a version before v12, GraphPlot works better (you get the desired/expected output). $\endgroup$
    – kglr
    Aug 27, 2019 at 23:48
  • $\begingroup$ Ya, unfortunately, I updated to v12 and I am thinking about reverting back now. $\endgroup$
    – Tirain
    Aug 28, 2019 at 0:00
  • $\begingroup$ do you have multi-edges in your edge list? $\endgroup$
    – kglr
    Aug 28, 2019 at 0:14
  • $\begingroup$ I ended up taking your original suggestion and reverted back to 11.3 and this solved all of the issues I was having with GraphPlot. Thanks for the info. $\endgroup$
    – Tirain
    Aug 28, 2019 at 2:06
  • $\begingroup$ user5277691, please see the update to my answer. $\endgroup$
    – kglr
    Aug 28, 2019 at 8:38

2 Answers 2

1
$\begingroup$

You could do

GraphPlot[{{2 -> -2, "a"}, {-2 -> 2, "b"}, {2 -> 6, "b"}, {6 -> 8, 
   "b"}, {8 -> 10, "b"}, {10 -> 8, "c"}}, DirectedEdges -> True, 
 EdgeShapeFunction -> "Line"]
$\endgroup$
1
$\begingroup$

Update: Re "reverted back to 11.3 and this solved all of the issues I was having with GraphPlot."

Losing quiet a few convenient features (including -- unlike Graph -- handling labeling of multi-edges correctly) with the v12 update to GraphPlot probably dismayed many users and WRI must have anticipated that. Following the hunch that the old GraphPlot can not possibly be completely inaccessible in v12, a simple search revealed GraphComputation`GraphPlotLegacy.

Hence no need to down-grade to v11 to be able to use the legacy GraphPlot and no need to look for work-arounds to replicate the legacy functionality ... just use GraphComputation`GraphPlotLegacy:

GraphComputation`GraphPlotLegacy[{{2 -> -2, "a"}, {-2 -> 2, "b"}, {2 -> 6, "b"}, 
   {6 -> 8, "b"}, {8 -> 10, "b"}, {10 -> 8, "c"}},
  MultiedgeStyle -> 1/2, BaseStyle -> 14, ImageSize -> 600]

enter image description here

With multi-edges

GraphComputation`GraphPlotLegacy[{{2 -> -2, "a"}, {2 -> - 2,  "b"},
    {2 -> - 2, "c"}, {2 -> 6, "b"}, {6 -> 8, "b"}, {8 -> 10, "w"},
    {8 -> 10, "x"}, {8 -> 10, "y"}, {8 -> 10, "z"}}, 
   MultiedgeStyle -> 1/2, BaseStyle -> 14, ImageSize -> 600]

enter image description here

Original answer:

If you don't have multi-edges, you can replace -> with \[DirectedEdge] in your edge list (or use the option DirectedEdges->True as in halmir's answer) and add the option EdgeStyle -> Arrowheads[0]:

el = {{2 -> -2, "a"}, {-2 -> 2, "b"}, {2 -> 6, "b"}, {6 -> 8, "b"},
   {8 -> 10, "b"}, {10 -> 8, "c"}}/. Rule -> DirectedEdge;

GraphPlot[el, EdgeStyle -> Arrowheads[0]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Well, thanks for the info this is also super helpful. $\endgroup$
    – Tirain
    Aug 28, 2019 at 13:03

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.