问题
I'm using sns.scatterplot
function to analyze some data. It would be very helpful for me if I could pick an object on the plot by clicking on it and execute a function. Matplotlib has onpick event which does the trick, but I couldn't find how could I do the same with Seaborn. It is using Matplotlib internally, so I think that it is possible somehow to attach onpick
handler to it.
The reason I'm using Seaborn instead of basic Matplotlib plot is that I need hue
parameter.
Here is basically the code that I'm using:
import seaborn as sns
import matplotlib.pyplot as plt
def _onpick(event):
# ... process selected item
print("Picked!")
tips = sns.load_dataset("tips")
ax = sns.scatterplot(x="total_bill", y="tip", data=tips)
# how to assign an 'onpick' callback
plt.show()
回答1:
Just as in any other case, you define the picker
argument and connect the callback function.
import seaborn as sns
import matplotlib.pyplot as plt
def onpick(event):
# ... process selected item
print("Picked!")
tips = sns.load_dataset("tips")
ax = sns.scatterplot(x="total_bill", y="tip", hue="time", data=tips, picker=4)
ax.figure.canvas.mpl_connect("pick_event", onpick)
plt.show()
来源:https://stackoverflow.com/questions/56032111/how-to-make-items-clickable-onpick-in-seaborn-scatterplot