plt.bar不连接单击事件(或任何事件)-python matplotlib

plt.bar不连接单击事件(或任何事件)-python matplotlib,python,matplotlib,plot,jupyter-notebook,bar-chart,Python,Matplotlib,Plot,Jupyter Notebook,Bar Chart,我一直在网上寻找这个例子,它们似乎都差不多。当我尝试一个小示例时,它是有效的,但由于某些原因,我没有让plt.bar工作。(我用的是jupyter笔记本) 下面是我显示图表的方式: fig = plt.figure(figsize=(12, 10)) xpos = np.arange(len(df.T.mean())) plt.bar(xpos, df.T.mean(), yerr=yerr, color=bar_colors, width=1, capsize=30, picker=True

我一直在网上寻找这个例子,它们似乎都差不多。当我尝试一个小示例时,它是有效的,但由于某些原因,我没有让plt.bar工作。(我用的是jupyter笔记本)

下面是我显示图表的方式:

fig = plt.figure(figsize=(12, 10))

xpos = np.arange(len(df.T.mean()))
plt.bar(xpos, df.T.mean(), yerr=yerr, color=bar_colors, width=1, capsize=30, picker=True )
以下是我的点击功能:

def on_click(event):
    plt.cla()
    plt.gca().set_title("please just work")
下面是我如何连接它的:

fig.canvas.mpl_connect('pick_event', on_click)
我还尝试了以下方法:

fig.canvas.mpl_connect('button_press_event', on_click)
fig.canvas.mpl_connect('motion_notify_event', on_click)
fig.canvas.mpl_connect('axes_enter_event', on_click)

#or the same with plt.gcf()
plt.gcf().canvas.mpl_connect('pick_event', on_click)
如何在条形图上检测单击事件

编辑

Server Information:
You are using Jupyter notebook.

The version of the notebook server is 4.2.3 and is running on:
Python 3.6.2 | packaged by conda-forge | (default, Jul 23 2017, 22:59:30) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]

Current Kernel Information:
Python 3.6.2 | packaged by conda-forge | (default, Jul 23 2017, 22:59:30) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
我忘了澄清,我只是想在这一点上连接点击事件。是的,我的点击事件现在应该只清除图表,但根本没有发生任何事情

第二次编辑

Server Information:
You are using Jupyter notebook.

The version of the notebook server is 4.2.3 and is running on:
Python 3.6.2 | packaged by conda-forge | (default, Jul 23 2017, 22:59:30) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]

Current Kernel Information:
Python 3.6.2 | packaged by conda-forge | (default, Jul 23 2017, 22:59:30) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

这段代码在使用
笔记本
后端的jupyter笔记本上运行良好

如果这对您不起作用,您可能必须从
%matplotlib inline
切换到
%matplotlib notebook

def on_click(event):
    plt.gca().set_title("Click at {:.2f}/{:.2f}\non {:s}".format(event.mouseevent.x, event.mouseevent.y, event.artist.__repr__()))
    plt.gcf().canvas.draw_idle()  # doesn't seem to be absolutely required,
                                  # but doesn't hurt to put it in


fig, ax = plt.subplots()
ax.bar(np.arange(10), np.arange(10), picker=True)
fig.canvas.mpl_connect('pick_event', on_click)

更改画布后,您忘记了重新绘制画布。我被您的
on\u click()
函数中的
plt.cla()
弄糊涂了。如果你清除轴,那么就没有更多的艺术家可以选择,因此你的功能就不能被触发了。我只是想从功能中看到任何东西。所以我期待一张空白的图表。这是我能想到的最直观的想法。很抱歉,我只是指定了我现在要做的就是连接点击事件。我只是想看到一些事情发生,然后我会做出必要的调整。您可能还没有选择任何交互式后端。我不会依赖画布本身重画。请参阅第一条评论。很公平,我的机器上不需要它,但可能这就是导致操作出现问题的原因。您是否使用笔记本后端(%matplotlib notebook)或内联后端(%matplotlin内联)?当然,我更新了我的答案。虽然你在问题中说,你可以得到一些简单的例子工作,只有条形图不工作,这表明后端不是我看到的问题,现在有意义了。很高兴我能帮忙