鼠标单击事件后刷新图

编程入门 行业动态 更新时间:2024-10-27 12:35:22
本文介绍了鼠标单击事件后刷新图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

单击鼠标后,我需要刷新matplotlib条形图.该图应采用event.ydata值并根据其重新绘制数据.我能够从鼠标事件中检索到该值,但是当我尝试刷新绘图时,似乎什么都没有发生.这是我的代码:

I need to refresh a matplotlib bar plot after a mouse click. The plot should take the event.ydata value and replot data according to it. I was able to retrieve this value from the mouse event, but when I try to refresh the plot nothing seems to happen. Here is my code:

#df is a pd.DataFrame, I have to plot just the df_mean with error bars (marginOfError) df_mean = df.mean(axis=1) df_std = df.std(axis=1) marginOfError = 1.96*df_std/np.sqrt(3650) index = np.arange(4) def print_data(threshold): colors = [] for i,err in zip(df_mean,marginOfError): if(i + err < threshold): colors.append('#001f7c') elif(i - err > threshold): colors.append('#bc0917') else: colors.append('#e0d5d6') fig, ax = plt.subplots() plt.bar(index, df_mean, 0.85, alpha=0.85, color=colors, yerr=marginOfError) plt.xticks(index, df.index) #horizontal threshold line ax.plot([-0.5, 3.5], [threshold, threshold], "c-") # first print data with a given threshold value print_data(df_mean.iloc[1]) def onclick(event): plt.gca().set_title('{}'.format(event.ydata)) print_data(event.ydata) plt.gcf().canvas.mpl_connect('button_press_event', onclick) fig.canvas.draw()

我在做什么错了?

推荐答案

您可能希望在同一图中绘制每个新图.因此,您应该在重复调用的函数之外创建图形和轴.因此,将 fig,ax = plt.subplots()放置在函数外部,并在内部绘制 ax.clear()轴,然后再绘制新图.

You would probably want to plot each new plot in the same figure. Therefore you should create the figure and axes outside the repetitively called function. Thus put fig, ax = plt.subplots() outside the function, and inside you may clear the axes, ax.clear() before drawing a new plot to it.

最后,您需要通过实际放置画布来重绘

At the end, you need to actually redraw the canvas by putting

plt.gcf().canvas.draw_idle()

onclick 函数末尾的

.

更多推荐

鼠标单击事件后刷新图

本文发布于:2023-10-08 04:21:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   单击   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!