Python 如何在没有jupyter的情况下将此交互式绘图导出到浏览器中查看?
我有一个python交互式绘图:Python 如何在没有jupyter的情况下将此交互式绘图导出到浏览器中查看?,python,plotly,jupyter,ipywidgets,Python,Plotly,Jupyter,Ipywidgets,我有一个python交互式绘图: import ipywidgets as widgets import plotly.graph_objects as go from numpy import linspace def leaf_plot(sense, spec): fig = go.Figure() x = linspace(0,1,101) x[0] += 1e-16 x[-1] -= 1e-16 positive = sense*x/(
import ipywidgets as widgets
import plotly.graph_objects as go
from numpy import linspace
def leaf_plot(sense, spec):
fig = go.Figure()
x = linspace(0,1,101)
x[0] += 1e-16
x[-1] -= 1e-16
positive = sense*x/(sense*x + (1-spec)*(1-x))
#probability a person is infected, given a positive test result,
#P(p|pr) = P(pr|p)*P(p)/P(pr)
# = P(pr|p)*P(p)/(P(pr|p)*P(p) + P(pr|n)*P(n))
# = sense*P(p)/( sense*P(p) +(1-spec)*P(n))
negative = 1-spec*(1-x)/((1-sense)*x + spec*(1-x))
fig.add_trace(
go.Scatter(x=x, y = positive, name="Positive",marker=dict( color='red'))
)
fig.add_trace(
go.Scatter(x=x, y = negative,
name="Negative",
mode = 'lines+markers',
marker=dict( color='green'))
)
fig.update_xaxes(title_text = "Base Rate")
fig.update_yaxes(title_text = "Post-test Probability")
fig.show()
sense_ = widgets.FloatSlider(
value=0.5,
min=0,
max=1.0,
step=0.01,
description='Sensitivity:',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='.2f',
)
spec_ = widgets.FloatSlider(
value=0.5,
min=0,
max=1.0,
step=0.01,
description='Specificity:',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='.2f',
)
ui = widgets.VBox([sense_, spec_])
out = widgets.interactive_output(leaf_plot, {'sense': sense_, 'spec': spec_})
display(ui, out)
如何将其导出,以便在浏览器中将其视为独立的网页(如HTML),同时保留互动性(如中)
使用plotly的fig.write_html()选项,我得到了一个独立的网页,但这样我就失去了滑块
经过一些修改,plotly最多允许使用一个滑块(plotly地物对象中不包括IPyWidget)
此外,在plotly中,所述滑块基本上控制预先计算的轨迹的可见性(参见示例),这限制了交互(有时参数空间很大)
最好的方式是什么
(我不必坚持使用plotly/ipywidgets)使用plotly,在创建图形后,保存它:
fig.write_html("path/to/file.html")
也可以在函数中尝试此参数:
动画选项:dict或None(默认无)
要传递给函数的自定义动画参数的dict
Plotly.js中的Plotly.animate。看见
获取可用选项。如果图形不包含,则无效
帧,或自动播放为假
否则,请查看此处以获得一些建议:您需要对事情进行一些修改,但您可以通过和实现您想要的 首先,需要修改leaf_plot()以返回地物对象
from numpy import linspace
def leaf_plot(sense, spec):
fig = go.Figure()
x = linspace(0,1,101)
x[0] += 1e-16
x[-1] -= 1e-16
positive = sense*x/(sense*x + (1-spec)*(1-x))
negative = 1-spec*(1-x)/((1-sense)*x + spec*(1-x))
fig.add_trace(
go.Scatter(x=x, y = positive, name="Positive",marker=dict( color='red'))
)
fig.add_trace(
go.Scatter(x=x, y = negative,
name="Negative",
mode = 'lines+markers',
marker=dict( color='green'))
)
fig.update_layout(
xaxis_title="Base rate",
yaxis_title="After-test probability",
)
return fig
然后编写dash应用程序:
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
html.H1("Interpreting Test Results"),
dcc.Graph(id='graph'),
html.Label([
"sensitivity",
dcc.Slider(
id='sensitivity-slider',
min=0,
max=1,
step=0.01,
value=0.5,
marks = {i: '{:5.2f}'.format(i) for i in linspace(1e-16,1-1e-16,11)}
),
]),
html.Label([
"specificity",
dcc.Slider(
id='specificity-slider',
min=0,
max=1,
step=0.01,
value=0.5,
marks = {i: '{:5.2f}'.format(i) for i in linspace(1e-16,1-1e-16,11)}
),
]),
])
# Define callback to update graph
@app.callback(
Output('graph', 'figure'),
Input("sensitivity-slider", "value"),
Input("specificity-slider", "value")
)
def update_figure(sense, spec):
return leaf_plot(sense, spec)
# Run app and display result inline in the notebook
app.run_server()
如果您在jupyter笔记本中执行此操作,您将只能在本地访问您的应用程序
如果您想发布,可以尝试是!您需要使用OP中指出的绘图指定保存函数中的类型,这会失去很多交互性。所有的滑块和其他iPyWidget都不见了。它们不是plotly figure对象的一部分。您知道这里使用的是什么后端吗:?