【发布时间】:2020-11-04 13:12:38
【问题描述】:
我在 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/(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,同时保留交互性,例如在https://gabgoh.github.io/COVID/index.html?
使用 plotly 的 fig.write_html() 选项,我得到一个独立的网页,但这样我会丢失滑块。
经过一些修改,plotly 最多允许单个滑块(ipywidgets 不包含在 plotly 图形对象中)。
另外,在情节上,上述滑块基本上控制了预先计算的轨迹的可见性(参见例如https://plotly.com/python/sliders/),这限制了交互性(有时参数空间很大)。
最好的方法是什么?
(我不一定需要坚持使用 plotly/ipywidgets)
【问题讨论】:
-
是的!您需要在保存功能中使用 plotly 指定类型
标签: python plotly jupyter ipywidgets