Python 鼠标悬停在绘图上的某些点上,用于在鼠标悬停在某个点上时显示数据,可显示某些点的数据,但不显示其他点的数据
我对以下绘图代码有一个奇怪的问题:Python 鼠标悬停在绘图上的某些点上,用于在鼠标悬停在某个点上时显示数据,可显示某些点的数据,但不显示其他点的数据,python,python-3.x,matplotlib,mouseover,interactive,Python,Python 3.x,Matplotlib,Mouseover,Interactive,我对以下绘图代码有一个奇怪的问题: topfolder="test" cfeature="READ_COUNT_RNR" plotsensitives=[0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.7666666666666667, 0.7666666666666667, 0.6666666666666666, 0.6666666666666666, 0.23333333333333334, 0.23333333333333334,
topfolder="test"
cfeature="READ_COUNT_RNR"
plotsensitives=[0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.7666666666666667, 0.7666666666666667, 0.6666666666666666, 0.6666666666666666, 0.23333333333333334, 0.23333333333333334, 0.23333333333333334, 0.23333333333333334, 0.16666666666666666, 0.16666666666666666]
plot1mspecificities=[0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7, 0.7, 0.2666666666666667, 0.2666666666666667, 0.23333333333333328, 0.23333333333333328, 0.16666666666666663, 0.16666666666666663]
seed=[57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0]
cutoff=[10.0, 10.0, 30.0, 30.0, 50.0, 50.0, 75.0, 75.0, 100.0, 100.0, 150.0, 150.0, 250.0, 250.0, 500.0, 500.0, 750.0, 750.0, 1000.0, 1000.0]
pangiaver=['test']*20
labels=["{0},{1},{2},{3},{4}".format(round(100*plotsensitives[pos],2),round(100*plot1mspecificities[pos],2),seed[pos],cutoff[pos],pangiaver[pos]) for pos in range(len(plot1mspecificities))]
annotations = [None for label in labels]
import matplotlib.pyplot as plt
import math
##defining size of markers:
markersize = 5
markersize_inches = markersize/72.
fig,ax = plt.subplots()
sc = plt.scatter(plot1mspecificities,plotsensitives)
plt.title('Receiver Operating Characteristic for the feature {0} in the {1} dataset'.format(cfeature,topfolder))
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
trans = ax.transData+fig.dpi_scale_trans.inverted()
##function for checking mouse coordinates and annotating
## need to define xdata,ydata
xdata,ydata=plotsensitives,plot1mspecificities
def on_move(event):
if event.inaxes:
x0, y0 = trans.transform((event.xdata, event.ydata))
#user coordinates=event.xdata, event.ydata, these are values seen by user, plot coordinates x0, y0, unseen coordinates used by plot
xfig, yfig = zip(*[trans.transform((x,y)) for x,y in zip(xdata,ydata)])
dists = [math.sqrt((x-x0)**2+(y-y0)**2) for x,y in zip(xfig, yfig)]
for n,(x,y,dist,label) in enumerate(zip(xdata,ydata,dists, labels)):
if dist < markersize_inches and annotations[n] is None:
annotations[n]=ax.annotate(
label,
[x,y], xycoords='data',
xytext = (10,10), textcoords='offset points',
ha='left', va='center',
bbox=dict(facecolor='white', edgecolor='black', boxstyle='round'),
zorder=10,
)
fig.canvas.draw()
elif dist > markersize_inches and annotations[n] is not None:
annotations[n].remove()
annotations[n] = None
fig.canvas.draw()
##connecting the event handler
cid = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()
编辑2:
中提到的另一种方法是,使用matplotlib在鼠标上方的散点图上用x、y坐标以外的标签标记点,这似乎是一个不错的解决方案,但它可以扩展到许多点,尽管在实践中不需要使用弹出标签查看10000个点 关于为散点图制作悬停注释的一般方法,已在中进行了询问和解答。接受的解决方案将适用于所有积分。@DyZ感谢您的回复。你说得对,当你只想用它的坐标来标记时,这个方法是有效的,但是如果你想给每个点添加额外的元数据呢?请参阅我在原始版本下面制作的修改版本。问题是如何使此代码适用于任意数量的条目?链接到的方法显示了如何添加其他数据(在这种情况下,图片中的字母“B”)。
import matplotlib.pyplot as plt
topfolder="test"
cfeature="READ_COUNT_RNR"
plotsensitives=[0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.7666666666666667, 0.7666666666666667, 0.6666666666666666, 0.6666666666666666, 0.23333333333333334, 0.23333333333333334, 0.23333333333333334, 0.23333333333333334, 0.16666666666666666, 0.16666666666666666]
plot1mspecificities=[0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7666666666666666, 0.7, 0.7, 0.2666666666666667, 0.2666666666666667, 0.23333333333333328, 0.23333333333333328, 0.16666666666666663, 0.16666666666666663]
seed=[57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0, 57.0, 75.0]
cutoff=[10.0, 10.0, 30.0, 30.0, 50.0, 50.0, 75.0, 75.0, 100.0, 100.0, 150.0, 150.0, 250.0, 250.0, 500.0, 500.0, 750.0, 750.0, 1000.0, 1000.0]
pangiaver=['test']*20
tempdata=[[round(100*entry,2) for entry in plot1mspecificities],[round(100*entry,2) for entry in plotsensitives],seed,cutoff,pangiaver]
transposeddata=list(map(list,zip(*tempdata)))
fig,ax = plt.subplots()
sc = plt.scatter(plot1mspecificities,plotsensitives, s=10) #cmap=cmap, norm=norm)
annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
def update_annot(ind):
pos = sc.get_offsets()[ind["ind"][0]]
alldatafrompoint=transposeddata[ind["ind"][0]]
annot.xy = alldatafrompoint
text=("{}, "*len(pos))[:-2].format(*alldatafrompoint)
annot.set_text(text)
def hover(event):
vis = annot.get_visible()
if event.inaxes == ax:
cont, ind = sc.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
else:
if vis:
annot.set_visible(False)
fig.canvas.draw_idle()
fig.canvas.mpl_connect("motion_notify_event", hover)
plt.show()