#header-mark#
Plotly 图表
PnPlotly
组件允许在 Panel 应用程序中显示 Plotly 图表。它通过对 Plotly 对象中包含的数组数据使用二进制序列化来提高图表更新速度。
请注意,要在 Jupyter 笔记本中使用 Plotly 组件,必须激活 Panel 扩展并包含 "plotly"
作为参数。这一步确保正确设置 plotly.js。
底层实现为panel.pane.Plotly
,参数基本一致,参考文档:https://panel.holoviz.org/reference/panes/Plotly.html
基本用法
让我们创建一个基本示例:
创建后,PnPlotly
组件可以通过分配新的图形对象来更新:
布局示例
PnPlotly
组件支持任意复杂度的布局和子图,允许显示即使是深度嵌套的 Plotly 图形:
响应式图表
通过在 Plotly 布局上使用 autosize
选项和 PnPlotly
组件的响应式 sizing_mode
参数,可以使 Plotly 图表具有响应性:
图表配置
您可以通过 config
参数设置 Plotly 配置选项。让我们尝试配置 scrollZoom
:
尝试在图表上用鼠标滚动!
增量更新
您可以通过使用字典而不是 Plotly Figure 对象来高效地增量更新轨迹或布局,而不是更新整个 Figure。
请注意,增量更新只有在将 Figure
定义为字典时才会高效,因为 Plotly 会复制轨迹,这意味着原地修改它们没有效果。修改数组将仅发送该数组(使用二进制协议),从而实现快速高效的更新。
事件处理
PnPlotly
组件提供对 Plotly 事件的访问,如点击、悬停和选择(使用Box Select
、Lasso Select
工具)等:
API
属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
object | 正在显示的 Plotly Figure 或字典对象 | object | None |
config | 图表的额外配置。参见 Plotly 配置选项 | dict | {} |
link_figure | 当 Plotly Figure 原地修改时更新显示的 Plotly 图表 | boolean | True |
click_data | 来自 plotly_click 事件的点击事件数据 | dict | {} |
clickannotation_data | 来自 plotly_clickannotation 事件的点击注释事件数据 | dict | {} |
hover_data | 来自 plotly_hover 和 plotly_unhover 事件的悬停事件数据 | dict | {} |
relayout_data | 来自 plotly_relayout 事件的重新布局事件数据 | dict | {} |
restyle_data | 来自 plotly_restyle 事件的重新样式事件数据 | dict | {} |
selected_data | 来自 plotly_selected 和 plotly_deselect 事件的选择事件数据 | dict | {} |
viewport | 当前视口状态,即显示图表的 x 和 y 轴限制。在 plotly_relayout 、plotly_relayouting 和 plotly_restyle 事件时更新 | dict | {} |
viewport_update_policy | 用户交互期间更新视口参数的策略 | str | 'mouseup' |
viewport_update_throttle | 当 viewport_update_policy 为 "throttle" 时,视口更新同步的时间间隔(毫秒) | int | 200 |
sizing_mode | 尺寸调整模式 | str | 'fixed' |
width | 宽度 | int, str | None |
height | 高度 | int, str | None |
min_width | 最小宽度 | int | None |
min_height | 最小高度 | int | None |
max_width | 最大宽度 | int | None |
max_height | 最大高度 | int | None |
margin | 外边距 | int, tuple | 5 |
css_classes | CSS类名列表 | list | [] |
Events
事件名 | 说明 | 类型 |
---|---|---|
click | 当元素被点击时触发的事件 | Callable |
hover | 当元素被鼠标覆盖时触发 | Callable |
selected | 当元素被Box Select 、Lasso Select 工具选中时触发 | Callable |
doubleclick | 当元素被双击时触发的事件 | Callable |
clickannotation | 当元素被鼠标覆盖时触发 | Callable |
Slots
插槽名 | 说明 |
---|---|
default | 自定义默认内容 |
Controls
src/examples/panel_vuepy/panes/Plotly