TutteInstitute / datamapplot

Creating beautiful plots of data maps
MIT License
449 stars 29 forks source link

[Question] Place label on centroid in static plot #9

Open cakiki opened 4 months ago

cakiki commented 4 months ago

I really like the way labels are placed on the clusters themselves in dynamic plots. Is there a way to do that for static plots?

cakiki commented 4 months ago

This might also help make the map less cluttered when there's a large number of labels

lmcinnes commented 4 months ago

I actually started with this approach, but struggled when there are a lot of labels -- if the text was large enough to read the plot got quite obscured. For the interactive plots I found this worked well because you can let the user zoom and reveal more labels as you go, so at any time there aren't that many labels on screen.

I can have a look at whether it might be tractable to add this as an option to the static plots since it can work when you don't have many labels, but I admit to not being keen on that approach in general.

lmcinnes commented 2 months ago

@MNoichl made a package called pylabeladjust that actually does a really good job of very gently nudging labels until they fit. This made the labels on the plot tractable. I've put together a basic version available on this branch. You can enable it with the option label_over_points=True to create_static_plot. It will definitely struggle if you get too many labels, or ask for the labels to have a large font size, but it works for a surprisingly broad range of cases (which is really all Max's doing). If you have some time I would appreciate if you could try it out and provide any feedback.

MNoichl commented 2 months ago

Quick heads up: I noticed a problem with pylabeladjust mishandling texts that were not left & bottom aligned. Didn't matter for the other use-cases, but here it was a problem. It should be fixed now.

MNoichl commented 2 months ago

Digging into your code, @lmcinnes, turns out it wasn't a problem, because you were using the rectangles_adjusted[["x_center", "y_center"]] values. I had removed them in the update because they seemed superfluous. I have re-added them now, so from version 0.1.13 things should work again.

cakiki commented 2 months ago

This is great, thank you both! Huge fan of gephi, so I'm very happy about the cross-polination. I will take this out for a spin. :smiley_cat:

lmcinnes commented 2 months ago

It pairs very well with (and updated version of) dynamic_label_size=True, so be sure to try out that setting as well. Thanks for taking the time to look at it.