I can’t seem to understand why the below coding does not give me the behaviour I am looking for.
The coding below draws a graph and once I click on the legend Icon for the drawn graph, I expect 2 lines of text to appear: 1)”non static event handler method working” & 2) “static event handler method working”.
However, once I click on the legend icon it is only the 2nd statement that appears on the terminal. This indicates that for some reason Python does not execute the event handler function, “nonstatic_onpick”, possibly because it has been provided as a non static method. Why is this the case? Have I made a syntax error somewhere? Is there a way to provide an event handler function as a non satic method?
(I understand that the below coding may not be the most elegant/efficient/readable algorithm but I have actually adapted this coding from another more complex use case that requires the algorithm to be in the current structure shown below.)
Coding:
import matplotlib.pyplot as plt
class Setup_IntrctveLgnd():
def __init__(self,legend,fig):
lgndicons=legend.legend_handles
lgndicons[0].set_picker(20)
fig.canvas.mpl_connect("pick_event", self.nonstatic_onpick)
fig.canvas.mpl_connect("pick_event", Setup_IntrctveLgnd.static_onpick)
def nonstatic_onpick(self,event):
print("non static event handler method working")
def static_onpick(event):
print("static event handler method working")
fig,ax=plt.subplots()
ax.plot([0,1,2,3], label="data")
legend=plt.legend()
Setup_IntrctveLgnd(legend,fig)
plt.show()
Any help would be much appreciated.
Hassan Hoque is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.