Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用MPLC游标在同一图形上为多个数据集添加标签_Python_Matplotlib_Annotations_Cursor - Fatal编程技术网

Python 使用MPLC游标在同一图形上为多个数据集添加标签

Python 使用MPLC游标在同一图形上为多个数据集添加标签,python,matplotlib,annotations,cursor,Python,Matplotlib,Annotations,Cursor,我有两组数据,我正在使用matplotlib在同一个绘图上绘制。我使用mplcursors使用标签数组对每个点进行注释。不幸的是,mplcursors对这两个数据集都使用前五个标签。我的问题是如何让第二个数据集拥有自己的自定义标签 我意识到对于这个简单的例子,我可以合并数据,但是对于我正在进行的项目,我不能 import matplotlib.pyplot as plt import mplcursors import numpy as np x = np.array([0, 1, 2, 3,

我有两组数据,我正在使用matplotlib在同一个绘图上绘制。我使用mplcursors使用标签数组对每个点进行注释。不幸的是,mplcursors对这两个数据集都使用前五个标签。我的问题是如何让第二个数据集拥有自己的自定义标签

我意识到对于这个简单的例子,我可以合并数据,但是对于我正在进行的项目,我不能

import matplotlib.pyplot as plt
import mplcursors
import numpy as np

x = np.array([0, 1, 2, 3, 4])
y = np.array([1, 2, 3, 4, 5])
y2 = np.array([2, 3, 4, 5, 6])

fig, ax = plt.subplots()
ax.plot(x, y, "ro")
ax.plot(x, y2, 'bx')

labels = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

mplcursors.cursor(ax, hover=True).connect(
    "add", lambda sel: sel.annotation.set_text(labels[sel.target.index]))
plt.show()

多亏了欧内斯特,我才有了一个简单的例子。我将每个绘图的标签分别设置为“1”和“2”。然后,将鼠标悬停在某个点上时,使用以下命令将艺术家名称与标签进行比较:

选择艺术家获取标签()


我对使用词典的评论如下:

import matplotlib.pyplot as plt
import mplcursors
import numpy as np

x = np.array([0, 1, 2, 3, 4])
y = np.array([1, 2, 3, 4, 5])
y2 = np.array([2, 3, 4, 5, 6])

fig, ax = plt.subplots()
line1, = ax.plot(x, y, "ro")
line2, = ax.plot(x, y2, 'bx')

labels1 = ["a", "b", "c", "d", "e"]
labels2 = ["f", "g", "h", "i", "j"]

d = dict(zip([line1, line2], [labels1, labels2]))

mplcursors.cursor(ax, hover=True).connect(
    "add", lambda sel: sel.annotation.set_text(d[sel.artist][sel.target.index]))

plt.show()
表示
选择
具有
艺术家
属性。您可以使用此功能,例如查询带有相应标签的词典。
import matplotlib.pyplot as plt
import mplcursors
import numpy as np

x = np.array([0, 1, 2, 3, 4])
y = np.array([1, 2, 3, 4, 5])
y2 = np.array([2, 3, 4, 5, 6])

fig, ax = plt.subplots()
line1, = ax.plot(x, y, "ro")
line2, = ax.plot(x, y2, 'bx')

labels1 = ["a", "b", "c", "d", "e"]
labels2 = ["f", "g", "h", "i", "j"]

d = dict(zip([line1, line2], [labels1, labels2]))

mplcursors.cursor(ax, hover=True).connect(
    "add", lambda sel: sel.annotation.set_text(d[sel.artist][sel.target.index]))

plt.show()