Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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 在散点图(Matplotlib)的工具提示中,每个气泡显示一个标签_Python_Python 3.x_Matplotlib - Fatal编程技术网

Python 在散点图(Matplotlib)的工具提示中,每个气泡显示一个标签

Python 在散点图(Matplotlib)的工具提示中,每个气泡显示一个标签,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我有一个动画散点图,显示了多年来人均GDP x每个国家的预期寿命。 我想添加一个工具提示,当有人将鼠标悬停在气泡上时会出现,我想让它显示气泡所对应的国家的名称 我曾尝试使用MPLS游标来实现这一点,但在计算如何显示国家名称时遇到了困难,因为每个泡沫中的国家名称都不同 下面是情节: ax.scatter(y = data['lifeExpec'], x = data['gdp'], s = data['population']/40000, c = data['region

我有一个动画散点图,显示了多年来人均GDP x每个国家的预期寿命。 我想添加一个工具提示,当有人将鼠标悬停在气泡上时会出现,我想让它显示气泡所对应的国家的名称

我曾尝试使用MPLS游标来实现这一点,但在计算如何显示国家名称时遇到了困难,因为每个泡沫中的国家名称都不同

下面是情节:

ax.scatter(y = data['lifeExpec'],
    x = data['gdp'],
    s = data['population']/40000,
    c = data['region'].astype('category').cat.codes,
    cmap = cm.viridis,
    edgecolors = 'none',
    alpha = 0.5)

 c1 = mplcursors.cursor(ax, hover=True)
    @c1.connect("add")
    def _(sel):
        sel.annotation.set_text(<????>)
        sel.annotation.set(position=(-15,15), 
                           fontsize=8, 
                           ha='center', 
                           va='center')
mplcursors文档的一节指出,我可以简单地使用target.index来指示拾取的点的索引。以下是最终代码:

c1 = mplcursors.cursor(ax, hover=True)
    @c1.connect("add")
    def _(sel):
        sel.annotation.set_text(data['country'].iat[sel.target.index])
        sel.annotation.set(position=(-15,15), 
                           fontsize=8, 
                           ha='center', 
                           va='center')

“贴标签可能不是最好的方法……”MadPhysicast我编辑了这个问题,并添加了我尝试使用MPLS的内容。这个问题的解决方案原则上是在对的回答中给出的。
c1 = mplcursors.cursor(ax, hover=True)
    @c1.connect("add")
    def _(sel):
        sel.annotation.set_text(data['country'].iat[sel.target.index])
        sel.annotation.set(position=(-15,15), 
                           fontsize=8, 
                           ha='center', 
                           va='center')