alandefreitas / matplotplusplus

Matplot++: A C++ Graphics Library for Data Visualization 📊🗾
https://alandefreitas.github.io/matplotplusplus/
MIT License
4.11k stars 313 forks source link

Allow to specify graph node locations #218

Closed yurivict closed 2 years ago

yurivict commented 2 years ago

I need to draw a pre-placed directed graph. Node locations are already known.

I found the edge_labels function, but there is no edge_coordinates function.

alandefreitas commented 2 years ago

The graph is an abstract concept whose layout is opaque. If the layout is part of the data structure, it's no longer a graph only. We represent graphs with whatever locations a given layout algorithm gives us. Locations might change with new vertices and edges.

If you already know where the nodes are located, instead of relying on an abstract graph, it's conceptually easier to use this information and just create a scatter plot with each vertex at the correct location.

There's another issue or discussion about this somewhere in the repo.

yurivict commented 2 years ago

[...] it's conceptually easier to use this information and just create a scatter plot with each vertex at the correct location [...]

Scatter plot doesn't seem to support point labels.

yurivict commented 2 years ago

Alternatively, you can add a new graph layout mechanism that would simply use the user-defined layout.

alandefreitas commented 2 years ago

Scatter plot doesn't seem to support point labels.

What the graph object does is (i) define the layout, (ii) draw scatter plot, (iii) draw lines, and (iv) maybe draw labels. You can look for the TSP example, where the locations are already determined and the points, lines and labels and drawn in three consecutive lines.

Alternatively, you can add a new graph layout mechanism that would simply use the user-defined layout.

Defining the layout is the only expensive process here. There's no point in creating another plot type for the case where positions are known, which is equivalent to just drawing two or three consecutive plots.

The graph layout is just an enumeration that refers to a layout algorithm, not a static position. Opening the possibility of a lambda could make some sense but coming up with an API for setting the positions directly would just make networks pointless. It also changes the data structure and everyone would need a different API for their use case.