Jupyter notebook canvas 在使用 %matplotlib inline 时没有交互
Jupyter notebook canvas not interactive when using %matplotlib inline
我有以下代码。如果没有行 %matplotlib inline,jupyter notebook 会弹出一个额外的 window 来显示绘图并且它是交互式的(右下角会在您移动时更新 x 和 y 值鼠标)。使用行 %matplotlib inline,绘图不具有交互性。
我可以做些什么来让它在笔记本中交互?
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()
您可以使用 %matplotlib notebook
而不是 %matplotlib inline
。
如 IPython tutorial on plotting 中所述:
The matplotlib library also ships with %matplotlib notebook
command that allows interactive figures if your environment allows it.
(强调我的)
我有以下代码。如果没有行 %matplotlib inline,jupyter notebook 会弹出一个额外的 window 来显示绘图并且它是交互式的(右下角会在您移动时更新 x 和 y 值鼠标)。使用行 %matplotlib inline,绘图不具有交互性。
我可以做些什么来让它在笔记本中交互?
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()
您可以使用 %matplotlib notebook
而不是 %matplotlib inline
。
如 IPython tutorial on plotting 中所述:
The matplotlib library also ships with
%matplotlib notebook
command that allows interactive figures if your environment allows it.
(强调我的)