2

I'm trying to make an interactive plot in the jupyter notebook but i don't know exactly how to implement it. Having a dataframe i run a simple regression that is then plotted to see the distribution. I'd like to be able to hover one of the points and get data associated with this point. How can i do that? Right now i can only produce a static plot enter image description here

import pandas as pd
from sklearn import linear_model
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

net = pd.read_csv("network_ver_64.csv")
net = net[net.AWDT12 > 0]

x = net.LOAD_DAILY.values
y = net.AWDT12.values
x_lenght = int(x.shape[0])
y_lenght = int(y.shape[0])
x = x.reshape(x_lenght, 1)
y = y.reshape(y_lenght,1)
regr = linear_model.LinearRegression()
regr.fit(x, y)

plt.scatter(x, y,  color='black')
plt.plot(x, regr.predict(x), color='blue', linewidth=1)
plt.xticks(())
plt.yticks(())
plt.show()
4
  • 1
    As far as I know, you cannot get hover functionality with matplotlib. You have to try d3.js or bokeh Commented Mar 9, 2017 at 19:02
  • @MainulIslam nice. yeah i didn't know about those modules, ill check them out to learn about them Commented Mar 9, 2017 at 19:09
  • Plot.ly is also worth checking out for what you want to do. Commented Mar 10, 2017 at 6:30
  • 1
    @Mainul There is an in-built hover functionality in matplotlib, which is also available in jupyter with the notebook backend. Apart from that any kind of interaction may be achieved programmatically. See my answer below. Commented Mar 10, 2017 at 9:22

1 Answer 1

6

First of all it's clear that the %matplotlib inline backend does not allow for interaction, as it is inline (in the sense that the plots are images).

However even in the notebook you can get interaction using the %matplotlib notebook backend. A basic hover took is already implemented: Moving the mouse in the canvas shows the current mouse position in data coordinates in the lower right corner.

enter image description here

Of course you can obtain more sophisticated functionality by writing some custom code. E.g. we can modify the picking example a little bit as follows:

import matplotlib.pyplot as plt
%matplotlib notebook
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on points')

line, = ax.plot(np.random.rand(100), 'o', picker=5)  # 5 points tolerance
text = ax.text(0,0,"")
def onpick(event):
    thisline = event.artist
    xdata = thisline.get_xdata()
    ydata = thisline.get_ydata()
    ind = event.ind
    text.set_position((xdata[ind], ydata[ind]))
    text.set_text(zip(xdata[ind], ydata[ind]))

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

This now shows the data coordinates of the point the mouse has clicked.

enter image description here

You're pretty free to adapt this to any case you like and make it more pretty using the standard matplotlib tools.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.