我正在制作一些带有链接信息的mpld3图.我想点(当前显示工具提示)是可点击的.现在,我可以将HTML链接嵌入到工具提示中,但它们不可点击,因为如果您尝试将鼠标悬停在工具提示上,则工具提示会消失.这可能吗?
这是一个示例页面,展示了我所做的以及我的想法:http: //www.eso.org/~aginsbur/EAACTF/EAACTF_plots_long.html
编辑:根据接受的答案,我的解决方案是:
class ClickInfo(mpld3.plugins.PluginBase):
"""mpld3 Plugin for getting info on click
Comes from:
http://stackoverflow.com/a/28838652/814354
"""
JAVASCRIPT = """
mpld3.register_plugin("clickinfo", ClickInfo);
ClickInfo.prototype = Object.create(mpld3.Plugin.prototype);
ClickInfo.prototype.constructor = ClickInfo;
ClickInfo.prototype.requiredProps = ["id", "urls"];
function ClickInfo(fig, props){
mpld3.Plugin.call(this, fig, props);
};
ClickInfo.prototype.draw = function(){
var obj = mpld3.get_element(this.props.id);
urls = this.props.urls;
obj.elements().on("mousedown",
function(d, i){
window.open(urls[i], '_blank')});
}
"""
def __init__(self, points, urls):
self.points = points
self.urls = urls
if isinstance(points, matplotlib.lines.Line2D):
suffix = "pts"
else:
suffix = None
self.dict_ = …Run Code Online (Sandbox Code Playgroud) 我想在Html中转换python图.我已经参考了示例,我已将其更改为将绘图转换为Html页面.Below是我的代码:
import matplotlib as plta
plta.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import mpld3
fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
N = 100
scatter = ax.scatter(np.random.normal(size=N),
np.random.normal(size=N),
c=np.random.random(size=N),
s=1000 * np.random.random(size=N),
alpha=0.3,
cmap=plt.cm.jet)
ax.grid(color='white', linestyle='solid')
ax.set_title("Scatter Plot (with tooltips!)", size=20)
labels = ['point {0}'.format(i + 1) for i in range(N)]
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)
html_graph = mpld3.fig_to_html(fig)
with open('plot.html', 'w') as the_file:
the_file.write(html_graph)
Run Code Online (Sandbox Code Playgroud)
现在,当我运行上面的代码时,它会抛出错误,如下所示:
/usr/local/lib/python2.7/dist-packages/matplotlib/cbook/deprecation.py:106: MatplotlibDeprecationWarning: The axisbg attribute was deprecated in version 2.0. Use facecolor instead.
warnings.warn(message, …Run Code Online (Sandbox Code Playgroud) 我试图复制这个网站上的例子:http: //mpld3.github.io/examples/scatter_tooltip.html
但是我收到以下错误: Object of type 'ndarray' is not JSON serializable
我无法弄清楚我需要改变什么.
这是代码:
import matplotlib.pyplot as plt
import numpy as np
import mpld3
fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
N = 100
scatter = ax.scatter(np.random.normal(size=N),
np.random.normal(size=N),
c=np.random.random(size=N),
s=1000 * np.random.random(size=N),
alpha=0.3,
cmap=plt.cm.jet)
ax.grid(color='white', linestyle='solid')
ax.set_title("Scatter Plot (with tooltips!)", size=20)
labels = ['point {0}'.format(i + 1) for i in range(N)]
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)
mpld3.show()
Run Code Online (Sandbox Code Playgroud)
确切的错误是:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pydev\pydev_run_in_console.py", line 52, in run_file …Run Code Online (Sandbox Code Playgroud) 我想将mpld3图的宽度和高度设置为特定值(以像素为单位,因此适合其所在的div)。我尝试的方式如下所示(javascript):
commands["width"]=plotWidth;
commands["height"]=plotHeight;
mpld3.draw_figure("plotname",commands);
Run Code Online (Sandbox Code Playgroud)
plotWidth和plotHeight是我想要将高度和宽度设置为的值。
现在,这实际上将mpld3-figure对象的大小设置为我想要的值,但是里面的绘图仍保持其旧大小,因此看起来什么也没发生。那么,如何更改地块本身的大小?到目前为止,无论我做什么,情节都不会改变。