python-igraph icon indicating copy to clipboard operation
python-igraph copied to clipboard

matplotlib backend does not support label_dist

Open szhorvat opened this issue 3 years ago • 6 comments

The matplotlib backend should support label_dist.

Example:

ig.config['plotting.backend']='matplotlib'  # also try with cairo
g = ig.Graph.Formula('ho - cus - po - kus')
ig.plot(g, vertex_label=g.vs['name'], vertex_label_dist=2)

This should offset the vertex labels.

szhorvat avatar Dec 12 '21 21:12 szhorvat

@iosonofabio If it is too complicated to do with Matplotlib, let me know and we can simply say that this is not supported in Matplotlib or Plotly.

ntamas avatar Dec 12 '21 21:12 ntamas

It's hard to do properly, i.e. keep the right offset upon zoom-in. I'll look into it though, it's time I do 😄

iosonofabio avatar Dec 12 '21 22:12 iosonofabio

It would be good to support putting labels below / above / next to vertices in some way (not necessarily in an identical way to how it is done with Cairo). Otherwise, using long names will be problematic. Here's the plot from the tutorial:

szhorvat avatar Dec 13 '21 08:12 szhorvat

Long names will always be problematic, but see here:

https://github.com/Phlya/adjustText

iosonofabio avatar Dec 13 '21 08:12 iosonofabio

For inspiration, what Mathematica does is that it considers the vertex and the label as two objects with their own bounding box. Then you set an anchor point in each, and the two points will be placed on top of each other. By default the vertex has an anchor at the top right and the label at the bottom left, resulting in this:

image

You can think of the anchor within the vertex as the "label position" and the anchor within the label as the "label alignment". For example, if the label anchor is at the centre left, then the label is left-aligned.

The anchor positions are specified using "scaled" coordinates which run from 0 to 1 within the bounding box of the object. Thus putting both anchors at 0.5, 0.5 centres the label on the vertex.

image

The values we give may be outside of the range of 0..1 though. For example, setting the label anchor to {-1, 0.5} and the vertex anchor to {0.5, 0.5} puts a label-sized gap between the vertex centre and the label. Using {0, 0.5} for the label and {2, 0.5} for the vertex puts a vertex-sized gap between lthe left edge of the label and the right edge of the vertex.

In normal usage, we do not specify these, but simply use what you might call "presets" such as Below, Left, Bottom, etc.

image

This system gives great flexibility in relative positioning. Of course, making labels avoid each other is a different topic.

It would be a good idea to look at what each of the other systems do, and what people might want to achieve, and make sure that our solution is flexible and fits real use cases.

szhorvat avatar Dec 13 '21 08:12 szhorvat

Thanks, check out the link above as well. Depending on where the edges are, each vertex label might want a different alignment and anchor relative to the vertex itself. But the real issue is zoomin within interactive environments (e.g. Tk,Qt5, Gtk3) which is a strong reason to use mpl in the first place.

iosonofabio avatar Dec 13 '21 09:12 iosonofabio