下面是使用mplcursors注释的散点图代码,它使用两列,用第三列标记点。
如何为单个文本框中的注释文本选择来自单个数据from的两列的两个值?
当注释文本框中不只是"name"时,我希望"height"和"name"都显示在注释文本框中。使用df[['height', 'name']]不起作用。
若非如此,如何才能做到呢?
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()发布于 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.4mplcursors v0.5.1中创建一个string,Selection.target.index不推荐,使用Selection.index代替。df.iloc[x.index, :]而不是df.iloc[x.target.index, :]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()

https://stackoverflow.com/questions/68276345
复制相似问题