【问题标题】:Seaborn and mplcursorsSeaborn 和 mplcursors
【发布时间】:2019-04-23 23:44:32
【问题描述】:

我想在散点图上绘制一些数据,并显示每个点的相关标签。数据看起来像

xlist=[1,2,3,4]
ylist=[2,3,4,5]
labels=['a', 'b', 'c', 'd']

我可以使用 Seaborn 绘图并尝试使用 mplcursor,但显示的标签是 x 和 y 而不是标签。

sns.scatterplot(x, y)
mplcursors.cursor(hover=True)

如何让它显示标签,而不是 (x, y)

【问题讨论】:

    标签: python-3.x matplotlib seaborn scatter-plot mplcursors


    【解决方案1】:

    您需要阅读the mplcursors documentation 并将关于该问题的示例从其中复制到您的代码中。让我帮你做吧:

    import matplotlib.pyplot as plt
    import seaborn as sns
    import mplcursors
    
    xlist=[1,2,3,4]
    ylist=[2,3,4,5]
    labels=['a', 'b', 'c', 'd']
    
    sns.scatterplot(xlist, ylist)
    
    cursor = mplcursors.cursor(hover=True)
    cursor.connect(
        "add", lambda sel: sel.annotation.set_text(labels[sel.target.index]))
    
    plt.show()
    

    【讨论】:

    • 对不起 - 我投了反对票,因为我认为这不起作用,但意识到需要添加 %matplotlib notebook 并且确实如此。对不起!
    • @jdc 如果是这种情况,请移除 DV 和 UV。
    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 2020-11-12
    • 2018-12-06
    • 2021-11-01
    • 2020-07-06
    • 1970-01-01
    • 2021-11-13
    • 2021-10-29
    相关资源
    最近更新 更多