tips for creating Graph diagrams

后端 未结 3 1284
感情败类
感情败类 2020-12-14 10:53

I\'d like to programmatically create diagrams like this
(source: yaroslavvb.com)

I imagine I should use GraphPlot with VertexCoordinateRules, Verte

3条回答
  •  隐瞒了意图╮
    2020-12-14 11:13

    For simple examples where you are only connecting two nodes (like your example on the far right), you can draw lines with capped end points like this.

    vertices = {a, b};
    Coordinates = {{0, 0}, {1, 1}};
    GraphPlot[{a -> b}, VertexLabeling -> True, 
     VertexCoordinateRules -> 
      MapThread[#1 -> #2 &, {vertices, Coordinates}], 
     Prolog -> {Blue, CapForm["Round"], Thickness[.1], Line[Coordinates]}]
    

    Mathematica graphics

    For more complex examples (like second from the right) I would recommend drawing a polygon using the vertex coordinates and then tracing the edge of the polygon with a capped line. I couldn't find a way to add a beveled edge directly to a polygon. When tracing the perimeter of the polygon you need to add the coordinate of the first vertex to the end of the line segment that the line makes the complete perimeter of the polygon. Also, there are two separate graphics directives for lines CapForm, which dictates whether to bevel the ends of the line, and JoinForm, which dictates whether to bevel the intermediate points of the line.

    vertices = {a, b, c};
    Coordinates = {{0, 0}, {1, 1}, {1, -1}};
    GraphPlot[{a -> b, b -> c, c -> a}, VertexLabeling -> True, 
     VertexCoordinateRules -> 
      MapThread[#1 -> #2 &, {vertices, Coordinates}], 
     Prolog -> {Blue, CapForm["Round"], JoinForm["Round"], Thickness[.15],
        Line[AppendTo[Coordinates, First[Coordinates]]], 
       Polygon[Coordinates]}]
    

    Mathematica graphics

提交回复
热议问题