Python Forum
Matplotlib Hovering Event displaying (x, y) data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matplotlib Hovering Event displaying (x, y) data
#1
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
https://stackoverflow.com/questions/7908...matplotlib

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
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)')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b-%y'))

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


def update_annot(ind):
    print(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"]]))
    annot.set_text(text)
    annot.get_bbox_patch().set_alpha(1.0)


def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        cont, ind = sc.contains(event)
        if cont:
            update_annot(ind)
            annot.set_visible(True)
            fig.canvas.draw_idle()
        else:
            if vis:
                annot.set_visible(False)
                fig.canvas.draw_idle()


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)
plt.show()
Reply


Messages In This Thread
Matplotlib Hovering Event displaying (x, y) data - by RawlinsCross - Oct-18-2019, 06:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to get base64Encoded Matplotlib Data Morkus 0 1,419 Jun-10-2021, 06:58 PM
Last Post: Morkus
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,943 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,216 Mar-11-2021, 10:52 AM
Last Post: buran
  matplotlib recursion error when repeatedly displaying a surface AdeIsHere 0 1,922 Sep-19-2019, 04:36 PM
Last Post: AdeIsHere
  Pygal: Displaying information for each data point KirkmanJ 0 1,834 Jul-29-2019, 01:10 PM
Last Post: KirkmanJ
  Matplotlib contour no data coordinates b4rtt 1 2,637 Jun-18-2019, 09:36 AM
Last Post: b4rtt
  Problems displaying data sent from Arduino to Pi VostokRising 2 2,312 May-18-2019, 08:28 PM
Last Post: VostokRising
  How do i get matplotlib event handling to work? jenya56 3 5,299 Apr-24-2019, 06:53 PM
Last Post: jenya56

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020