Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python单击事件的Plotly(脱机)_Python_Jupyter Notebook - Fatal编程技术网

Python单击事件的Plotly(脱机)

Python单击事件的Plotly(脱机),python,jupyter-notebook,Python,Jupyter Notebook,我需要在Jupyter的Plotly(脱机)中获取单击事件 我的处理方法是使用javascript和以下命令将值返回到python: var kernel = IPython.notebook.kernel; kernel.execute(command); …其中的命令类似于变量=xxxxx(就像) 我在尝试用python用HTML绘制图表时被绊住了(注意,我可以通过这种方式成功地加载jQuery): 从IPython.display导入HTML HTML(“”) 格拉菲科 变量trace

我需要在Jupyter的Plotly(脱机)中获取单击事件

我的处理方法是使用javascript和以下命令将值返回到python:

var kernel = IPython.notebook.kernel;
kernel.execute(command);
…其中的命令类似于
变量=xxxxx
(就像)

我在尝试用python用HTML绘制图表时被绊住了(注意,我可以通过这种方式成功地加载jQuery):

从IPython.display导入HTML
HTML(“”)
格拉菲科

变量trace1={ x:[1,2,3,4], y:[10,15,13,17], 模式:“标记” }; 变量trace2={ x:[2,3,4,5], y:[16,5,11,10], 模式:“行” }; 变量trace3={ x:[1,2,3,4], y:[12,9,15,12], 模式:“行+标记” }; var数据=[trace1,trace2,trace3]; var布局={}; Plotly.newPlot('myDiv',数据,布局); ''')
错误消息是:

引用错误:未定义Plotly 在评估时(在globalEval时评估)(jquery.min.js:2),:21:17) 评估时() 位于Function.globalEval(jquery.min.js:2) 在ua(jquery.min.js:3) 在n.fn.init.append(jquery.min.js:3) 在outputrea.\u safe\u append(outputrea.js:456) 在OutputArea.append\u execute\u结果(OutputArea.js:493) 在outputrea.append_输出(outputrea.js:326) 在outputrea.handle_输出(outputrea.js:257) 在输出端(codecell.js:382)


我已经设法从这个网站上获得点击点

从plotly.offline导入下载\u plotlyjs,初始化\u笔记本\u模式,绘图,iplot
导入plotly.graph_objs作为go
从plotly导入工具
作为pd进口熊猫
将numpy作为np导入
从日期时间导入日期时间
初始笔记本模式(已连接=真)
从IPython.core.display导入显示,HTML
N=30
random_x=np.random.randn(N)
random_y=np.random.randn(N)
选择=[]
#创建跟踪
trace=go.Scatter(
x=随机_x,
y=随机_y,
模式='标记'
)
数据=[跟踪]
#打印并嵌入ipython笔记本!
plot=plot(数据,filename='basic-scatter',包括\u plotlyjs=False,输出\u type='div')
divId=plot.split(“id=\”,1)[1]。split(“”,1)[0]
plot=plot.replace(“Plotly.newPlot”、“var-graph=Plotly.newPlot”)
绘图=绘图。替换(“,”)
var-graph=document.getElementById(“”“+divId+”);
var color1='#7b3294';
var color1Light='#c2a5cf';
var colorX='#ffa7b5';
var colorY='#fdae61';
var kernel=IPython.notebook.kernel;
;graph.on('plotly_selected',函数(eventData){
var x=[];
变量y=[];
var颜色=[];
对于(var i=0;i<%i;i++)颜色。按下(color1Light);
eventData.points.forEach(函数(pt){
x、 推力(第x部分);
y、 推力(第y部分);
颜色[点编号]=颜色1;
var comando='selected.append('+pt.x+','+pt.y+')'
控制台日志(comando);
execute(comando);
});
Plotly.restyle(图形,'marker.color',[colors],[0]);
});
“”%N)
显示(HTML(打印))
在本例中,我们能够在Javascript和Python之间进行双向通信。请注意,创建图表的数据来自Python。选择点后,我将一个元组附加到
selected
变量,该变量属于Python范围

from IPython.display import HTML
HTML('''
    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
        </head>
        <body>
            <h3>Gráfico</h3>
            <hr>
            <div id="myDiv"></div>
            <script>

                var trace1 = {
                  x: [1, 2, 3, 4],
                  y: [10, 15, 13, 17],
                  mode: 'markers'
                };

                var trace2 = {
                  x: [2, 3, 4, 5],
                  y: [16, 5, 11, 10],
                  mode: 'lines'
                };

                var trace3 = {
                  x: [1, 2, 3, 4],
                  y: [12, 9, 15, 12],
                  mode: 'lines+markers'
                };

                var data = [ trace1, trace2, trace3 ];
                var layout = {};
                Plotly.newPlot('myDiv', data, layout);
            </script>
        </body>
    </html>
''')
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
from plotly import tools
import pandas as pd
import numpy as np
from datetime import datetime
init_notebook_mode(connected=True)
from IPython.core.display import display, HTML


N = 30
random_x = np.random.randn(N)
random_y = np.random.randn(N)

Chosen = []

# Create a trace
trace = go.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers'
)

data = [trace]

# Plot and embed in ipython notebook!
plot = plot(data, filename='basic-scatter', include_plotlyjs=False, output_type='div')
divId=plot.split("id=\"",1)[1].split('"',1)[0]
plot = plot.replace("Plotly.newPlot", "var graph = Plotly.newPlot")
plot = plot.replace("</script>", """
var graph = document.getElementById('"""+divId+"""');
var color1 = '#7b3294';
var color1Light = '#c2a5cf';
var colorX = '#ffa7b5';
var colorY = '#fdae61';
var kernel = IPython.notebook.kernel;
;graph.on('plotly_selected', function(eventData) {
  var x = [];
  var y = [];

  var colors = [];
  for(var i = 0; i < %i; i++) colors.push(color1Light);

  eventData.points.forEach(function(pt) {
    x.push(pt.x);
    y.push(pt.y);
    colors[pt.pointNumber] = color1;
    var comando = 'Chosen.append((' + pt.x + ', ' + pt.y + '))'
    console.log(comando);
    kernel.execute(comando);

  });


  Plotly.restyle(graph, 'marker.color', [colors], [0]);
});
""" % N)
display(HTML(plot))