Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Matplotlib Hovering Event displaying (x, y) data
Hey, I'm an Unladed Swallow - how bout that!. Anyhooo….I have an excel graph that has x data in column A starting in row 7 and several columns of y data. I search out a given column through a unique identifier and do an x,y scatter plot. The thing is I want to be able to hover on a point and for it to show the x,y data.

I recently found an article on another forum that does something similar

I'm trying to adapt that code to mine and I'm having a rough time. Here's what I have done so far. Think the problem is in the list of lists? Not sure, new to python.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np; np.random.seed(1)
from pandas.plotting import register_matplotlib_converters

# Excel graph stored on a desktop
data = pd.read_excel(r'c:/Users/jhalfyard/Desktop/HydroMetPro.xlsx')
sDataIdentifier = 'Aqu_223-IR2-FEED_Co'
lastrow = data.shape[0]
diloc = data.columns.get_loc(sDataIdentifier)
# Data starts on row 7
x = data.iloc[7:lastrow, 0]
y = data.iloc[7:lastrow, diloc]
names = np.array(list("A")*(data.shape[0] - 7))

fig, ax = plt.subplots()
sc = plt.scatter(x, y, s=5, color='blue')
fig.autofmt_xdate()  # Rotates and auto-formats the date
fig.canvas.set_window_title(sDataIdentifier)  # Sets the figure title
ax.set_ylabel('Concentration (mg/L)')

annot = ax.annotate("", xy=(0,0), xycoords='data', xytext=(15,15), textcoords="offset points",
                    bbox=dict(boxstyle="round", fc='white'),

def update_annot(ind):
    pos = sc.get_offsets()[ind["ind"][0]]
    annot.xy = pos
    text = "{}, {}".format(",".join(list(map(str,y))),
                          "".join([names[n] for n in ind["ind"]]))

def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        cont, ind = sc.contains(event)
        if cont:
            if vis:

fig.canvas.mpl_connect("motion_notify_event", hover)

plt.subplots_adjust(top=0.95, bottom=0.16)  # Adjust the top of the graph (default = 0.90)


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  matplotlib recursion error when repeatedly displaying a surface AdeIsHere 0 275 Sep-19-2019, 04:36 PM
Last Post: AdeIsHere
  Pygal: Displaying information for each data point KirkmanJ 0 324 Jul-29-2019, 01:10 PM
Last Post: KirkmanJ
  Matplotlib contour no data coordinates b4rtt 1 592 Jun-18-2019, 09:36 AM
Last Post: b4rtt
  Problems displaying data sent from Arduino to Pi VostokRising 2 419 May-18-2019, 08:28 PM
Last Post: VostokRising
  How do i get matplotlib event handling to work? jenya56 3 1,544 Apr-24-2019, 06:53 PM
Last Post: jenya56

Forum Jump:

Users browsing this thread: 1 Guest(s)