3

I'm plotting data using seaborn's swarm method. After the plot is drawn, I'd like to retrieve the (x,y)-coordinates of the drawn points.

My application for this is: I want to add a legend to the plot, and automatically (I have a large number of plots…) decide whether to display the legend inside or outside the plot. For this, I'd first draw the legend inside, get its bounding box, and check whether any of the drawn points overlap that bounding box. If that is the case, redraw the whole plot with the legend outside.

Is it possible to retrieve the coordinates of the drawn points? I tried iterating over the axes's get_children(), but there seem to be other artists in there, which are not points and which cause my legend to go outside even if it would not overlap with any data points.

Looking at the code, I don't see any obvious way of doing this, since the coordinates from the beeswarm() method don't seem to be stored anywhere, but perhaps I missed something?

Thanks in any case,

Lukas

Edit: Here's a minimal example:

test_data = pd.DataFrame([
    {'x': 1, 'y': 1.0, 'color': 1.0},
    {'x': 1, 'y': 2.0, 'color': 0.5},
    {'x': 1, 'y': 2.8, 'color': 0.25},
    {'x': 1, 'y': 4.0, 'color': 0.125},

    {'x': 2, 'y': 1.0, 'color': 1.0},
    {'x': 2, 'y': 2.0, 'color': 0.5},
    {'x': 2, 'y': 2.7, 'color': 0.25},
    {'x': 2, 'y': 4.0, 'color': 0.125},

    {'x': 3, 'y': 1.0, 'color': 1.0},
    {'x': 3, 'y': 2.0, 'color': 0.5},
    {'x': 3, 'y': 2.7, 'color': 0.25},
    {'x': 3, 'y': 4.0, 'color': 0.125},
])

p = sns.swarmplot(data=test_data, x='x', y='y', hue='color')
legend = p.legend(frameon=True)
legend.get_frame().set_fc('gray')

This produces this image: Plot with one point overlapping the legend

I'd like to detect that the legend overlaps the top-rightmost point and draw it outside the plot.

Lukas Barth
  • 2,734
  • 18
  • 43
  • There is for sure a solution. I'm just a little confused by what a legend to a swarmplot would be. Usually the axes labels would directly tell how to interprete the points. So to give you a matching solution, it would be good to have a [mcve] available (maybe one from the documenation matches your case? else create some small sample with the required legend present). – ImportanceOfBeingErnest Jan 22 '18 at 16:01
  • 3
    Maybe [this answer](https://stackoverflow.com/questions/36615354/obtaining-span-of-plotted-points-from-seaborn-swarmplot) is helpful. – Automaton2000 Jan 22 '18 at 16:19
  • The legend is used to map the colors of the points (used to display a third variable) to values. I've added an example. – Lukas Barth Jan 22 '18 at 16:21
  • Yea, the answer linked by @Automaton2000 pretty much solves the issue, right? Should we mark as duplicate or is there still something unclear? – ImportanceOfBeingErnest Jan 22 '18 at 16:24
  • Looks good! I'll try and see if that fixes my problem. If so, I'll let you know so you can mark it as a duplicate! – Lukas Barth Jan 22 '18 at 16:26
  • Seems to work! Flagged it as duplicate. – Lukas Barth Jan 22 '18 at 16:53

0 Answers0