Men*_*eng 11 python canvas matplotlib jupyter-notebook
我在test.py中有以下代码:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
def onclick(event):
print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
(event.button, event.x, event.y, event.xdata, event.ydata))
cid = fig.canvas.mpl_connect('button_press_event', onclick)
Run Code Online (Sandbox Code Playgroud)
当我在命令行中通过"python test.py"运行test.py时,'button =%d,x =%d,y =%d,xdata =%f,ydata =%f'被打印,因为我点击了情节
但是,结果不会在jupyter笔记本中打印出来.
怎么解决?
提前致谢!
Imp*_*est 17
它将取决于您在jupyter笔记本中使用的后端.
%matplotlib inline),则交互式功能无法工作,因为这些图只是png图像.如果您使用笔记本后端(即%matplotlib notebook)交互式功能确实有效,但问题是将结果打印到哪里.因此,为了显示文本,可以将其添加到图中,如下所示
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
text=ax.text(0,0, "", va="bottom", ha="left")
def onclick(event):
tx = 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (event.button, event.x, event.y, event.xdata, event.ydata)
text.set_text(tx)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
4658 次 |
| 最近记录: |