python散点图怎么设标签_python – 使用matplotlib在鼠标悬停的散点图上标记点,使用除x,y坐标之外的某些标签...

我正在尝试使用DataCursor方法(

https://stackoverflow.com/a/4674445/1301710)使用matplotlib标记点.我有几千点,并希望在鼠标悬停时看到他们的标签.但是,有两个不同之处:一,我正在制作散点图和两个,我想为每个点标注名称而不仅仅是x,y坐标.

这是我的代码

import os

import matplotlib.pyplot as plt

class DataCursor(object):

text_template = 'x: %0.2f\ny: %0.2f'

x, y = 0.0, 0.0

xoffset, yoffset = -20, 20

text_template = 'x: %0.2f\ny: %0.2f'

def __init__(self, ax, labels,x,y):

self.ax = ax

self.xlist = x

self.ylist = y

self.labels = labels

self.annotation = ax.annotate(self.text_template,

xy=(self.x, self.y), xytext=(self.xoffset, self.yoffset),

textcoords='offset points', ha='right', va='bottom',

bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),

arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')

)

self.annotation.set_visible(False)

def __call__(self, event):

self.event = event

xdata, ydata = event.artist.get_data()

#self.x, self.y = xdata[event.ind], ydata[event.ind]

self.x, self.y = event.mouseevent.xdata, event.mouseevent.ydata

self.label = self.labels[self.xlist.index(self.x)]

if self.x is not None:

self.annotation.xy = self.x, self.y

self.annotation.set_text(self.label)

self.annotation.set_visible(True)

event.canvas.draw()

def process():

#code to make ht_dict here

# ht_dict has the following format: 'ht1' = [nov14count, nov21count] where each key is a string and each value is a list of two integers

print("Start making scatter plot..")

hts = []

nov14 = []

nov21 = []

for key in ht_dict.keys():

nov14.append(ht_dict[key][0])

nov21.append(ht_dict[key][1])

hts.append(key)

fig = plt.figure()

scatter = plt.scatter(nov14, nov21)

fig.canvas.mpl_connect('pick_event', DataCursor(plt.gca(), hts, nov14, nov21))

scatter.set_picker(5)

plt.show()

process()

我收到以下错误:

AttributeError: 'CircleCollection' object has no attribute 'get_data'

我希望能够在鼠标悬停时看到存储在列表hts中的字符串,分别存储在相同索引处的nov14和nov21列表中的x和y坐标处.我不知道该怎么做这个错误,并希望得到任何帮助.

我的另一个问题是(从尝试对DataCursor线程中的现有绘图进行更改)使用索引来获取标签,因为我当前正在做的将给出一个值,因为单击的值可能在列表错误中不存在与列表中的值不完全相同.您是否有任何建议可以更好地显示某个点的标签/名称?

任何我可以阅读的文档的指导或指示将不胜感激.

谢谢!

最佳答案 在

mpldatacursor的文档页面的示例部分中给出了使用标签进行注释的方法,您可以沿着这些线做某事(用每个散点图绘制单个点以便能够为每个点设置单独的标签) :

import matplotlib.pyplot as plt

from mpldatacursor import datacursor

import random

fig, ax = plt.subplots()

ax.set_title('Click on a dot to display its label')

# Plot a number of random dots

for i in range(1, 1000):

ax.scatter([random.random()], [random.random()], label='$ID: {}$'.format(i))

# Use a DataCursor to interactively display the label for a selected line...

datacursor(formatter='{label}'.format)

plt.show()

不幸的是,它的效率相当低,即比1000点更难以使用.

结果示例图片:

TuAaV.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值