简体   繁体   English

如何在 python 中的 matplotlib 图中的 hover 上的给定列表中显示元素的索引号?

[英]How can I show element's index number from a given list on hover in matplotlib graph in python?

I am using mplcursors module to show label value.我正在使用mplcursors模块来显示 label 值。 I also want to show the index number from given lists for that particular point on which I will hover.我还想显示给定列表中我将 hover 的特定点的索引号。 My Example Code snippet:我的示例代码片段:

import matplotlib.pyplot as plt
import mplcursors
lines = plt.plot([1, 2, 3, 4], [2, 4, 6, 10])
plt.ylabel('some numbers')
mplcursors.cursor(hover=True)
plt.show()

Is there any way that I can use the mplcursors to annotate the required information(as its easy)?有什么方法可以使用mplcursors来注释所需的信息(因为它很容易)? Thanks:)谢谢:)

mplcursors allows to specify a function that is called every time just before an annotation is shown. mplcursors允许指定一个 function ,每次在显示注释之前调用它。 That function gets a parameter sel which among others has a target field. function 得到一个参数sel ,其中包括一个target字段。 In case of a "line", apart from the xy value, the target contains an index .在“线”的情况下,除了 xy 值之外,目标还包含一个index The integer part of the index is an index into the x and y arrays of the left end of the segment the cursor is on. index的 integer 部分是 cursor 所在段左端的xy arrays 的索引。 The fractional part of the index tells how far we are between the left and the right end of the segment. index的小数部分告诉我们在段的左端和右端之间有多远。

import matplotlib.pyplot as plt
import mplcursors

def show_annotation(sel):
    ind = int(sel.target.index)
    frac = sel.target.index - ind
    x, y = sel.target
    sel.annotation.set_text(f'left index:{ind} frac:{frac:.2f}\nx:{x:.2f} y:{y:.2f}')

lines = plt.plot([1, 2, 3, 4], [2, 4, 6, 10])
plt.ylabel('some numbers')
cursor = mplcursors.cursor(hover=True)
cursor.connect("add", show_annotation)
plt.show()

示例注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不知道索引号时如何从列表中删除元素 - How can I delete an element from a list when I do not know it's index number python:给定一个数字,如何打印列表中的字母表? - python: How can I print an alphabet from a list, given a number? 我如何在 matplotlib 中从数字列表中制作图表? - How can I in matplotlib make graph from list of numbers? 如何从 Python 的 Kivy 库中获取 Spinner 中特定元素的索引? - How can I get the index of a specific element in a Spinner from Python's Kivy library? python的列表方法`index()`是否可以在列表中找到列表元素 - can python's list method `index()` find a list element in a list 如何在Python中从给定的字符串中找到最大的数字? - How can I find the largest number from the given string in Python? 如何从日期时间列表(python3、pandas、matplotlib)生成图形? - How to generate a graph from datetime list (python3, pandas, matplotlib)? 如何从多维列表中的元素获取索引? - How can I get index from an element in a mulitidimensional list? Python | 给定每个索引的特定数字范围,如何将主列表中的值分组到不同列表 - Python | How to group value to different lists from a main list given a certain range of number for each index (python,matplotlib)如何在不是方程的图中进行微分? - (python, matplotlib) how can I do differential in graph that isn't from equation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM