2
$\begingroup$

The graph we drew below using tikz is easy.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\tikzstyle{every node} = [inner sep=2pt,circle,draw]
    \node (0) at (-8, 4) {};
    \node (1) at (-2, 4) {};
    \draw (0) to (1);
    \draw [in=105, out=15, looseness=1.50] (0) to (1);
    \draw [bend left, looseness=1.25] (1) to (0);
\end{tikzpicture}
\end{document} 

enter image description here

I'd like to draw this graph in Mathematica, but it seems that I can only control one edge. (The other two multiple edges seem to be out of control)

g = Graph[{1 <-> 2, 1 <-> 2, 1 <-> 2}, VertexLabels -> All, 
 EdgeShapeFunction -> {1 <-> 2 -> {"CurvedEdge", "Curvature" -> -3}}]

enter image description here

PS: The top edge of the graph drawn by tikz is a Bessel curve. Can Mathematica specify the type of such a curve. I looked in the help documentation of EdgeShapeFunctionand didn't see the type of curve selected.

I know that Mathematica is not a drawing software. But sometimes I need to add some special shaped edges to the resulting graph, like the link below.

$\endgroup$

2 Answers 2

5
$\begingroup$

Define custom edge function:

bezier[pts_, "Left", loose_ : .3] := bezier[pts, -30, 30 + 180, loose]

bezier[pts_, "Right", loose_ : .3] := bezier[pts, 30, -30 + 180, loose]

bezier[{x_, y_}, out_, in_, loose_ : .3] :=
 Block[{dir, p1, p2},
   dir = y - x;
   p1 = RotationTransform[out Degree, x][x + loose dir];
   p2 = RotationTransform[in Degree, y][y + loose dir];
   BezierCurve[{x,  p1, p2, y}]
 ]

Set graph with tagged edges:

Graph[{UndirectedEdge[1,2,1], UndirectedEdge[1,2,2], UndirectedEdge[
  1,2,3]}, VertexLabels -> {1 -> "1", 2 -> "2"}, 
 EdgeShapeFunction -> {UndirectedEdge[
    1,2,1] -> (bezier[#1[[{1, -1}]], 15, 105, .5] &), 
   UndirectedEdge[1,2,3] -> (bezier[#1[[{1, -1}]], "Left", .45] &)}, 
 VertexCoordinates -> {{-8, 4}, {-2, 4}}, 
 PlotTheme -> "IndexLabeled", VertexSize -> .05]

enter image description here

$\endgroup$
3
$\begingroup$

To control the edges separately, use distinct vertices that share their coordinates.

g = Graph[{1 <-> 2, 1 <-> 3, 1 <-> 4}, 
  VertexLabels -> {1 -> "1", 2 -> "2"}, 
  EdgeShapeFunction -> {
    1 <-> 2 -> {"CurvedEdge", "Curvature" -> 3}, 
    1 <-> 3 -> {"CurvedEdge", "Curvature" -> -2}},
  VertexCoordinates ->
   {{0, 0}, {1, 0}, {1, 0}, {1, 0}}]

enter image description here

$\endgroup$

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.