首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用mplcur标对多列进行注释

如何使用mplcur标对多列进行注释
EN

Stack Overflow用户
提问于 2021-07-06 19:20:01
回答 1查看 325关注 0票数 2

下面是使用mplcursors注释的散点图代码,它使用两列,用第三列标记点。

如何为单个文本框中的注释文本选择来自单个数据from的两列的两个值?

当注释文本框中不只是"name"时,我希望"height""name"都显示在注释文本框中。使用df[['height', 'name']]不起作用。

若非如此,如何才能做到呢?

代码语言:javascript
复制
df = pd.DataFrame(
    [("Alice", 163, 54),
     ("Bob", 174, 67),
     ("Charlie", 177, 73),
     ("Diane", 168, 57)],
    columns=["name", "height", "weight"])

df.plot.scatter("height", "weight")
mplcursors.cursor(multiple = True).connect("add", lambda sel: sel.annotation.set_text((df["name"])[sel.target.index]))
plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-16 17:32:21

  • df.loc[sel.target.index, ["name", 'height']].to_string():正确地选择列和行和.loc,然后在 python 3.8**,** matplotlib 3.4.2**,** pandas 1.3.1**,和** jupyterlab 3.1.4
  • In mplcursors v0.5.1中创建一个stringSelection.target.index不推荐,使用Selection.index代替。
    • df.iloc[x.index, :]而不是df.iloc[x.target.index, :]

代码语言:javascript
复制
from mplcursors import cursor
import matplotlib.pyplot as plt
import pandas as pd

# for interactive plots in Jupyter Lab, use the following magic command, otherwise comment it out
%matplotlib qt 

df = pd.DataFrame([('Alice', 163, 54), ('Bob', 174, 67), ('Charlie', 177, 73), ('Diane', 168, 57)], columns=["name", "height", "weight"])

ax = df.plot(kind='scatter', x="height", y="weight", c='tab:blue')
cr = cursor(ax, hover=True, multiple=True)

cr.connect("add", lambda sel: sel.annotation.set_text((df.loc[sel.index, ["name", 'height']].to_string())))
plt.show()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68276345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档