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

Matplotlib and Cairo layouts do not look the same

Open vtraag opened this issue 3 years ago • 8 comments

Describe the bug The layout coordination systems in matplotlib and cairo are using inverted y-axis from each other.

To reproduce

g = ig.Graph.Famous('Zachary')
ig.plot(g)

yields

image

while

fig, ax = plt.subplots()
ig.plot(g, target=ax)

yields image

It would be nice if the two coordination systems would align. Of course, I understand that both systems have their own drawing systems and Cairo probably uses a top-left coordinate system (i.e. positive y-axis goes down in the picture) while matplotlib uses a figure coordinate system (i.e. positive y-axis goes up in the picture). To me, it seems most natural to stick to a matplotlib type coordinate system. Could we perhaps simply always invert the y coordinate before drawing in Cairo?

Version information Development version from develop branch.

vtraag avatar Dec 14 '21 22:12 vtraag

ax.invert_yaxis() before or after the plot inverts the y axis in mpl... would you like me to invert it by default?

iosonofabio avatar Dec 14 '21 22:12 iosonofabio

I think we should stick to the defaults with Matplotlib and invert the Cairo plot instead. I'd vote for that because it is not nice if igraph messes around with the axis direction of the ax object that the user passes to the plot function (unless we are careful and revert it when we are done, even if we exit with an exception).

ntamas avatar Dec 14 '21 22:12 ntamas

Cairo is also the lower level drawing interface, so there it makes sense that you setup your own coordinate system for drawing. Perhaps it even has direct support for custom coordinate systems?

vtraag avatar Dec 15 '21 05:12 vtraag

In images, the y coordinate is usually top to bottom, I guess that's that @ntamas has done there

On Wed, Dec 15, 2021, at 16:51, Vincent Traag wrote:

Cairo is also the lower level drawing interface, so there it makes sense that you setup your own coordinate system for drawing. Perhaps it even has direct support for custom coordinate systems?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/igraph/python-igraph/issues/482#issuecomment-994314980, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJFEAFMOU5O6AAN36DMPB3URAUFLANCNFSM5KCC6ZVA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

iosonofabio avatar Dec 15 '21 05:12 iosonofabio

Cairo has support for arbitrary transformation matrices so you can "push" a transformation to a Cairo context; this allows you to flip the axes, or scale them, or rotate/skew the whole thing. However, if we flip the Y axis permanently, we break the code for people who have drawn additional stuff on a Cairo context manually, plus we may potentially break the code that aligns labels above/below the vertices. I'd probably mess around with the call to layout.fit_into(...) in the graph drawing code; this is the part that takes the layout and scales the coordinates in a way that they fit into the bounding box that the user wants to draw into. Here we could flip the Y axis in the layout before mapping it to Cairo-space.

ntamas avatar Dec 15 '21 09:12 ntamas

invert_y=None -> Cairo only?

iosonofabio avatar Dec 15 '21 10:12 iosonofabio

Something like that, yes.

ntamas avatar Dec 15 '21 13:12 ntamas

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 24 '22 08:02 stale[bot]