2010-01-15 56 views

回答

2

当我最近做了这样的事情时,我从matplotlib返回了一个PNG到浏览器。然后,我使用与绘制的点对应的像素值将图像映射应用于PNG。我使用了一个onmouseover事件来弹出一个包含关于该点的元数据的'tooltip'(实际上只是一个绝对定位的div)。

这个article为我的努力提供了基础,但我记得他的实现中存在一定的问题(主要是由于matplotlib api的变化)。如果这个问题在周一还在徘徊,我会用我的实现中的特定代码更新这个答案(我目前没有访问我的工作机器)。

编辑:正如所承诺的示例代码

import matplotlib.pyplot as plt 

dpi = 96 
fig = plt.figure(figsize=(8,8),dpi=dpi) 
imgWidth = fig.get_figwidth() * dpi ## this is the actual pixel size of the plot 
imgHeight = fig.get_figheight() * dpi ## this is the actual pixel size 

my_lines = [] 
my_lines.append(plt.plot(Xs,Ys,marker='o')[0]) # add a line object to plot 

mapHTML = '<MAP name="curveMap">' 
for lineObj in my_lines: 
    ## convert the points to pixel locations, for pop-ups 
    lineObj._transform_path() 
    path, affine = lineObj._transformed_path.get_transformed_points_and_affine() 
    path = affine.transform_path(path) 
    for real,pixel in zip(lineObj.get_xydata(),path.vertices): 
    mapHTML+='''<AREA shape=\"circle\" coords=\"%i,%i,5\" href=\"javascript: void(0);\" onmouseout=\"outFly();\" onmouseover=\"javascript: popFly\(event,\\'%s\\',%i,%i\)\" />''' % (pixel[0],imgHeight-pixel[1],lineName,real[0],real[1]) 
mapHTML += '</MAP>' 
fig.savefig(file(plot_file,"w"),format='png',dpi=dpi) 
plt.close(fig) 
plotHTML = '''<img src="/getPlot?uniq=%f" width="%i" height="%i" ismap usemap="#curveMap" onload="imageLoadCallback();" id="curPlot" />''' % (time.time(),imgWidth,imgHeight) 
return "({'plotHTML':'%s','mapHTML':'%s'})" % (plotHTML,mapHTML) 

你会看到我写的图像为PNG文件,然后返回JSON。我使用JavaScript来更新新的img和图像映射的DIV。

2

下面是使用jQuery的解决方案:

$('#myChart').mousemove(function(e){ 
    var x = e.pageX - this.offsetLeft; 
    var y = e.pageY - this.offsetTop; 
    // Do something with x and y; 
}); 

http://docs.jquery.com/Tutorials:Mouse_Position了解更多信息。

相关问题