2
$\begingroup$

How can I set absolute vertex sizes in Graph, so that the vertex size on screen (number of pixels or printer's points) is invariant to changes in the option ImageSize? All of the types of setting in the documentation for VertexSize cause the on-screen size to change with ImageSize.

This is useful for example to match vertex sizes between multiple Graphs used in different panels of the same publication figure.

$\endgroup$

1 Answer 1

3
$\begingroup$

I do not think that it is possible to do this with VertexSize. The vertex sizes used by Graph are translated to plot coordinates, not to absolute coordinates.

A practical solution is to set a VertexShapeFunction that will ignore the sizes passed to it and renders at a constant absolute size.

SeedRandom[42]
g = RandomGraph[{30, 60}]
g = Graph[g, 
       VertexShapeFunction -> Function[{pt, v, size}, Disk[pt, Offset[5]]]];
Table[Show[g, ImageSize -> size], {size, {100, 200, 300}}]

enter image description here

The vertices are always rendered as disks having a radius of 5 pixels, regardless of the image size. You might want to add ImagePadding -> 5 to the above to make sure that there is always enough margin left to render these disks.

If the graph is directed, it is important to add

PerformanceGoal -> "Quality"

so that arrowheads will be positioned correctly.


Unfortunately, the technique above will often lead to parts of the vertices being cut off at the image borders. The ImagePadding must be increased manually to prevent this.

When a simple disc, without outlines, is sufficient, it is more convenient to use Point with AbsolutePointSize:

VertexShapeFunction -> ({AbsolutePointSize[15], Point[#1]} &)

This avoids the vertices being cropped.

$\endgroup$
3
  • $\begingroup$ Thanks. But what if I want to use a shape other than Disk? I see in the documentation that Inset will do the trick, but would prefer a cleaner solution. $\endgroup$ Commented Dec 16, 2018 at 1:56
  • $\begingroup$ @CarbonFlambe It does not matter what the shape it. As I said, all you need to do is write your own VertexShapeFunction that ignores the size argument and produces something in absolute coordinates (see Offset) $\endgroup$
    – Szabolcs
    Commented Dec 16, 2018 at 9:05
  • 1
    $\begingroup$ @CarbonFlambe--ReinstateMonica I updated the answer with some extra information. Actually, Inset might be the simplest way to handle arbitrary shapes ... $\endgroup$
    – Szabolcs
    Commented Jan 10, 2020 at 12:27

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.