How to make items clickable (onpick) in Seaborn scatterplot?

不打扰是莪最后的温柔 提交于 2021-02-11 14:26:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!