2
$\begingroup$

I want to reconstruct this picture,like this:

enter image description here

I have do some work,this is my code

u = {1, 1, 2, 3, 3, 2, 3, 2, 4, 4};
v = {3, 2, 1, 1, 2, 3, 4, 4, 3, 2};

weight = {1, 1, 0, 0, 0, 1, 1, 1, 0, 0};

edge = {
   EdgeWeight -> weight,
   EdgeStyle -> {RGBColor["#4472c7"]
     , Thick},
   EdgeLabels -> Placed["EdgeWeight", 0.5]};
    
vertex = {
   VertexStyle -> {_ -> White,
     Alternatives @@ {1} -> RGBColor["#538234"],
     Alternatives @@ {2, 3} -> RGBColor["#4473c3"],
     Alternatives @@ {4} -> RGBColor["#ed7d31"]
     },
   VertexLabels -> Placed["Name", Center],
   VertexLabelStyle -> {
     Alternatives @@ Range@4 -> 
      Directive[30, White, Bold, FontFamily -> "Times New Roman"]
     },
   VertexSize -> Large};

Graph[
  MapThread[DirectedEdge[#1, #2] &, {u, v}],
  edge,
  vertex,
  GraphLayout -> {"MultipartiteEmbedding", 
    "VertexPartition" -> {1, 2, 1}}
  ] // SetProperty[#, 
   VertexCoordinates -> 
    Thread[VertexList[#] -> ({1.3, 1} # & /@ 
        ReflectionTransform[{0, 1}][GraphEmbedding[#]])]] &

get this

enter image description here

By comparing two images:

enter image description here

My image is:

1.Edge is bend

2.Vertex color is more dim

I didn't know what to do next. Help me

$\endgroup$
2
  • 3
    $\begingroup$ If all you want is the picture, then just use a vector graphics tool (e.g. InkScape, or PowerPoint in a pinch): it will be a lot less work! $\endgroup$
    – MarcoB
    Jun 17 at 14:08
  • $\begingroup$ Add BaseStyle -> EdgeForm[] as Graph option to remove black borders from vertices and EdgeShapeFunction -> ({ Arrow[#1, .35]} &) to make edges straight (might need change to touch the vertices). $\endgroup$
    – Ben Izd
    Jun 17 at 14:27

2 Answers 2

8
$\begingroup$

Set up custom edge function:

myEdge[s_ : .25, offset_ : .04][coords_, _[a_, b_]] :=
 Block[{x = coords[[1]], y = coords[[-1]], normal},
  normal = Normalize[{-1, 1} Reverse[y - x]];
  Arrow[{coords[[1]] + offset normal, coords[[-1]] + offset normal}, 
   s]]

set EdgeForm might help for color:

u = {1, 1, 2, 3, 3, 2, 3, 2, 4, 4};
v = {3, 2, 1, 1, 2, 3, 4, 4, 3, 2};

weight = {1, 1, 0, 0, 0, 1, 1, 1, 0, 0};

edge = {EdgeWeight -> weight, 
   EdgeStyle -> Directive[RGBColor[{68, 115, 193, 255}/255], Thick], 
   EdgeLabels -> Placed["EdgeWeight", 0.5]};

vertex = {VertexStyle -> {_ -> White, 
     Alternatives @@ {1} -> 
      Directive[EdgeForm[RGBColor["#538234"]], RGBColor["#538234"]], 
     Alternatives @@ {2, 3} -> 
      Directive[EdgeForm[RGBColor["#4473c3"]], RGBColor["#4473c3"]], 
     Alternatives @@ {4} -> 
      Directive[EdgeForm[RGBColor["#ed7d31"]], RGBColor["#ed7d31"]]}, 
   VertexLabels -> Placed["Name", Center], 
   VertexLabelStyle -> {Alternatives @@ Range@4 -> 
      Directive[30, White, Bold, FontFamily -> "Times New Roman"]}, 
   VertexSize -> Large};

Graph[MapThread[DirectedEdge[#1, #2] &, {u, v}], edge, vertex, 
  GraphLayout -> {"MultipartiteEmbedding", 
    "VertexPartition" -> {1, 2, 1}}, EdgeShapeFunction -> myEdge[]] //
  SetProperty[#, 
   VertexCoordinates -> 
    Thread[VertexList[#] -> ({1.3, 1} # & /@ 
        ReflectionTransform[{0, 1}][GraphEmbedding[#]])]] &
$\endgroup$
5
$\begingroup$

Color appearance is influenced by what is around. For example, the vertices have thick edges of different colors, which I tried to reproduced below. I changed to color of the edges to match the target. If the font needs to be reproduced, there are better options than Times New Roman. Below I use a font downloaded from Google Fonts (Noto Serif). The arrows are made opaque to match the target, but both ends point to the center of the vertices (see halmir's answer to correct that).

u = {1, 1, 2, 3, 3, 2, 3, 2, 4, 4};
v = {3, 2, 1, 1, 2, 3, 4, 4, 3, 2};

weight = {1, 1, 0, 0, 0, 1, 1, 1, 0, 0};

edge = {EdgeWeight -> weight, EdgeStyle -> RGBColor["#4a74c1ff"], 
   EdgeLabels -> Placed["EdgeWeight", 0.5]};

vertex = {VertexStyle -> {_ -> White,
     Alternatives @@ {1} -> 
      Directive[RGBColor["#538234"], 
       EdgeForm[{Thick, RGBColor["#556f40"]}]],
     Alternatives @@ {2, 3} -> 
      Directive[RGBColor["#4473c3"], 
       EdgeForm[{Thick, RGBColor["#53739c"]}]],
     Alternatives @@ {4} -> 
      Directive[RGBColor["#ed7d31"], 
       EdgeForm[{Thick, RGBColor["#b58549"]}]]},
   VertexLabels -> Placed["Name", Center], 
   VertexLabelStyle -> (Alternatives @@ Range@4 -> 
      Directive[72, White, Bold, FontFamily -> "Noto Serif"]), 
   VertexSize -> Large};

Graph[MapThread[DirectedEdge[#1, #2] &, {u, v}], edge, vertex, 
  GraphLayout -> {"MultipartiteEmbedding", 
    "VertexPartition" -> {1, 2, 1}}, 
  EdgeShapeFunction -> ({Arrow[#1, 0.25]} &)] // 
 SetProperty[#, 
   VertexCoordinates -> 
    Thread[
     VertexList[#] -> ({1.3, 1} # & /@ 
        ReflectionTransform[{0, 1}][GraphEmbedding[#]])]] &

enter image description here

$\endgroup$

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.