在 bqplot 中动态调整轴

Dynamically adjust axes in bqplot

我正在开发基于 jupyter 的仪表板进行数据分析,并计划使用 bqplot 绘制数据。仪表板规范的一部分是能够调整轴,以便能够缩放 in/out 数据。到目前为止,我已经能够让它动态更新,而无需完全重新加载图形。有没有办法做到这一点?如果是这样,如何?以下是我大致意思的一个片段:

def updateXAxis(values):
    #Update X-axis min/max value here

x_sc = LinearScale(min=float(x_data[0]))
y_sc = LinearScale()

ax_x = Axis(label='X', scale=x_sc, grid_lines='solid', tick_format='0f')
ax_y = Axis(label='Y', scale=y_sc, orientation='vertical', tick_format='0.2f')

m_fig = dict(left=100, top=50, bottom=50, right=100)
fig = Figure(axes=[ax_x, ax_y], marks=data_values, fig_margin=m_fig)

x_range = IntRangeSlider(value=[0,1000],
                        min=0,
                        max=2000,
                        step=1,
                        description="X Axis",
                        disabled=False,
                        continuous_update=False,
                        orientation='horizontal',
                        readout=True)
interactive(updateXAxis, values=x_range)
fig

bqplot 中已经内置了用于平移和缩放图表的交互,我认为您不需要构建自己的交互。看看这个笔记本中的例子。

https://github.com/bloomberg/bqplot/blob/master/examples/Interactions/Interaction%20Layer.ipynb.

您需要添加额外的一行来构建交互。您可以在字典中将它传递给一个或两个尺度,我在这里将其限制为 x。然后在创建图形时将 PanZoom 对象与交互 kwarg 一起传递。

panzoom = PanZoom(scales={'x': [x_sc]})
fig = Figure(axes=[ax_x, ax_y], marks=[data_values], fig_margin=m_fig, interaction=panzoom)

完整示例:

from bqplot import *
from ipywidgets import *
import numpy as np

x_data = np.linspace(1,101)
y_data = np.linspace(1,101)
x_sc = LinearScale()
y_sc = LinearScale()

ax_x = Axis(label='X', scale=x_sc, grid_lines='solid', tick_format='0f')
ax_y = Axis(label='Y', scale=y_sc, orientation='vertical', tick_format='0.2f')

scatter = Scatter(x=x_data, y=y_data, scales={'x': x_sc, 'y': y_sc})

m_fig = dict(left=100, top=50, bottom=50, right=100)
panzoom = PanZoom(scales={'x': [x_sc]})
fig = Figure(axes=[ax_x, ax_y], marks=[scatter], fig_margin=m_fig, interaction=panzoom)

x_range = IntRangeSlider(value=[0,1000],
                        min=0,
                        max=2000,
                        step=1,
                        description="X Axis",
                        disabled=False,
                        continuous_update=False,
                        orientation='horizontal',
                        readout=True)

fig

这里的问题最终是 interactive 函数不是很灵活。相反,应该使用 observe,下面是一个例子:

def updateXAxis(values):
    if change['type'] == 'change' and change['name'] == 'value':
        x_sc.min = change['new'][0]
        x_sc.max = change['new'][1]

x_sc = LinearScale(min=float(x_data[0]))
y_sc = LinearScale()

ax_x = Axis(label='X', scale=x_sc, grid_lines='solid', tick_format='0f')
ax_y = Axis(label='Y', scale=y_sc, orientation='vertical', tick_format='0.2f')

m_fig = dict(left=100, top=50, bottom=50, right=100)
fig = Figure(axes=[ax_x, ax_y], marks=data_values, fig_margin=m_fig)

x_range = IntRangeSlider(value=[0,1000],
                        min=0,
                        max=2000,
                        step=1,
                        description="X Axis",
                        disabled=False,
                        continuous_update=False,
                        orientation='horizontal',
                        readout=True)

x_range.observe(updateXAxis)
widgets.VBox([fig, x_range])

我在此处提交的 git 问题中对此有更详细的回答:https://github.com/bloomberg/bqplot/issues/712