1

I have the following code. Without the line %matplotlib inline, jupyter notebook pops out an extra window to display the plot and it is interactive (the right bottom corner updates the x and y values as you move the mouse). With the line %matplotlib inline, the plot is not interactive.

Anything I can do to make it interactive within the notebook?

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
%matplotlib inline

origins = ['China','Brazil','India','USA','Canada','UK','Germany',
    'Iraq','Chile','Mexico']


df = pd.DataFrame({'height': 
np.random.rand(10),'weight':np.random.rand(10),
              'origin':origins})

plt.figure()
plt.scatter(df['height'],df['weight'],picker=5)
plt.gca().set_ylabel('Weight')
plt.gca().set_xlabel('Height')

def onpick(event):
    origin = df.iloc[event.ind[0]]['origin']
    plt.gca().set_title('came from {}'.format(origin))

plt.gcf().canvas.mpl_connect('pick_event',onpick)
plt.show()

1 Answer 1

1

You can use %matplotlib notebook instead of %matplotlib inline.

As explained in the IPython tutorial on plotting:

The matplotlib library also ships with %matplotlib notebook command that allows interactive figures if your environment allows it.

(Emphasis mine)

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.