0

I have a scatterplot with about 280,000 points which displays quite nicely. However, I would like to dynamic labels to each point so that when I zoom into the graph enough I can see a bit of text on each point.

I've tried just using plt.annotate on every point, and on a smaller number of points

for index, row in points.iterrows():
    plt.annotate(row[0],  (row[1], row[2]))
    #if index+1 %100 == 0:
    #    break

This causes the window to lag and not actually display anything rather than display labels. If I uncomment the break then I still have a rather laggy window with a large black clump of overlapping text. If the text could only be displayed under a certain level of magnification, or even scale to be an appropriate size at different levels of magnification that would be great!

I'm really open to any solutions to create a labeled scatter plot from my data.

Jetman
  • 765
  • 4
  • 14
  • 30
James
  • 91
  • 8
  • 1
    I'm not sure Matplotlib is the right tool for the job. Such dynamic adjustments sound more like a Javascript Graph Framework. If you want to stay in Python, you could take a look at the framework "Plotly". I think this interactive feature is supported. But I'm not sure how it works with 280k data points (Could possibly be a bit slow...) – g3n35i5 Jan 14 '19 at 07:23
  • Matplotlib could be the underlying tool for the job, but the zoom-adaptive part of it would have be done with a custom GUI. You can also have the xy_coord display pop up the values under the cursor. – Jody Klymak Jan 14 '19 at 18:06

1 Answers1

1

I was able to plot everything nicely with plotly like this, using plotly's Scattergl to speed everything up.

import plotly as plotly
py = plotly.offline
import plotly.graph_objs as go

trace = go.Scattergl(
    x = points['x'], 
    y = points['y'],
    text = points['word'], 
    mode = 'markers',
    marker = dict(
        color = '#FFBAD2',
        line = dict(width = 1)
    )
)

data = [trace]
layout = plotly.graph_objs.Layout(hovermode='closest')
figure = plotly.graph_objs.Figure(data=data, layout=layout)
py.plot(figure)
James
  • 91
  • 8
  • 1
    You didn't say you wanted a hover solution in the question, but with matplotlib that would look [like this](https://stackoverflow.com/questions/7908636/possible-to-make-labels-appear-when-hovering-over-a-point-in-matplotlib/47166787#47166787). – ImportanceOfBeingErnest Jan 14 '19 at 22:32