1

我经常使用 Python,但对 js 一无所知。

我想画一个图,左边是散点图,右边是几条曲线。

右边的每条曲线对应左边的一个点,而且只有一个点。

当我“鼠标悬停”一条线时,我希望相应的点突出显示。

那可能吗 ?

谁能给我一个我可以适应的例子?

非常感谢 !

拉斐尔

编辑 :

你是对的

但是我对mpld3一无所知(还)!

所以这里有一个例子,纯python。

from pylab import *

#3 Scattered points
x=[1,3,7]
y=[2,6,4]

#3 Curves (each one associated with a point)
xx=linspace(0,4*pi,100)
yy1=sin(xx)
yy2=cos(xx)
yy3=linspace(0,1,100)

#Left panel
f=figure()
f.add_subplot(121)
scatter(x,y)

#Right panel
f.add_subplot(122)
plot(xx,yy1)
plot(xx,yy2)
plot(xx,yy3)

show()

看到图片了吗?当我将鼠标移到右侧的曲线上时,我希望左侧的相应点突出显示。

现在我认为问题很清楚了。很抱歉,我(还)不能提出任何代码供您更正,但我想知道如何使用 mpld3 的人可能能够为我提供一个我可以适应的模板……</p>

谢谢你们 !

4

1 回答 1

0

First thing to keep in mind is that mpld3 translates (almost) whatever you do in matplotlib to javascript, so mouseover highlighting is not something that comes out of the box.

To begin with mpld3, substitute the show() in your snippet by:

htmlFig = str(mpld3.fig_to_html(f))

Save the output to a .html file and you can open your plot on the browser. (There is a mpld3.show() too if you prefer, but my point is showing you the javascript output).

Well, after that you have to search and test mpld3 plugins, which essentially modifies this html/js output to put d3 functionalities. For example, this plugin here https://mpld3.github.io/examples/random_walk.html, highlights the curve with mouseover, which is close to what you need. So, if you don't find a plugin that does exactly what you want, you have to do some tweaking in the output html/javascript file.

Sorry for the partial answer. I hope it can put you on the right track.

Cheers!

于 2017-03-03T17:06:21.067 回答