3
$\begingroup$

I want to plot my graph in a certain ratio(2:1), for example, 400*200(Width * Height).

I tried ImageSize:

GraphPlot[{2 -> 1, 2 -> 0, 3 -> 2, 3 -> 1, 4 -> 2, 4 -> 3, 5 -> 3, 
  5 -> 4}, DirectedEdges -> True, VertexLabels -> "Name", 
 ImageSize -> {400, 200}]

And outputs:

ImageSize

Mathematica just put my graph in the middle of the picture and it doesn't fit well

And then I tried ImageRotate and ImageResize:

ImageResize[
 ImageRotate[
  GraphPlot[{2 -> 1, 2 -> 0, 3 -> 2, 3 -> 1, 4 -> 2, 4 -> 3, 5 -> 3, 
    5 -> 4}, DirectedEdges -> True, VertexLabels -> "Name"], 
  90 Degree], {400, 200}]

Rotate

But the label is rotated also.

How can I output my graph in a 400*200 picture and fit well?

Edit:

GraphPlot[{2 -> 1, 2 -> 0, 3 -> 2, 3 -> 1, 4 -> 2, 4 -> 3, 5 -> 3, 
  5 -> 4}, DirectedEdges -> True, VertexLabels -> "Name", 
 AspectRatio -> 1/2]

AspectRatio

It appears to fit but in a strange way. I want to make it better and like this:

Example

$\endgroup$
5
  • 1
    $\begingroup$ AspectRatio? $\endgroup$
    – MarcoB
    Commented Nov 14 at 19:54
  • $\begingroup$ AspectRatio -> 1 $\endgroup$ Commented Nov 14 at 19:56
  • $\begingroup$ You need AspectRatio -> 1/2 $\endgroup$ Commented Nov 14 at 20:02
  • $\begingroup$ @MarcoB Can you check my edit? $\endgroup$
    – Sayalic
    Commented Nov 14 at 20:05
  • 2
    $\begingroup$ You need a different embedding method. E.g.: GraphLayout -> "DiscreteSpiralEmbedding" $\endgroup$ Commented Nov 14 at 20:41

1 Answer 1

11
$\begingroup$
  • we set VertexLabels -> Placed["Name", {Left, Bottom}] To make the labels looks good.

  • we replace all the coordinate {x,y} to {-y, -x} by {x_Real, y_Real} :> ReflectionTransform[{0, 1}]@RotationMatrix[-(π/2)] . {x, y} to rotate and reflect the graph(also affect the labels)

g = GraphPlot[{2 -> 1, 2 -> 0, 3 -> 2, 3 -> 1, 4 -> 2, 4 -> 3, 5 -> 3,
    5 -> 4}, DirectedEdges -> True, 
  VertexLabels -> Placed["Name", {Left, Bottom}], 
  ImageSize -> {400, 200}]
g /. {x_Real, y_Real} :> 
  ReflectionTransform[{0, 1}]@RotationMatrix[-(π/2)] . {x, y}

enter image description here

  • Or another style.
Clear[g];
 g = 
 GraphPlot[{2 -> 1, 2 -> 0, 3 -> 2, 3 -> 1, 4 -> 2, 4 -> 3, 5 -> 3, 
   5 -> 4}, DirectedEdges -> True, 
  VertexLabels -> Placed["Name", Center], ImageSize -> {400, 200}, 
  VertexShapeFunction -> 
   Function[{p}, {Red, AbsolutePointSize[20], Point[p], White, 
     AbsolutePointSize[15], Point[p]}]]
g /. {x_Real, y_Real} :> 
  ReflectionTransform[{0, 1}]@RotationMatrix[-(π/2)] . {x, y}

enter image description here

$\endgroup$
0

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.