在bqplot中动态调整轴

时间:2018-08-29 00:51:21

标签: jupyter-notebook jupyter bqplot

我正在使用基于jupyter的仪表板进行数据分析,并计划使用bqplot绘制数据。仪表板的部分规格是能够调整轴以能够放大/缩小数据。到目前为止,我已经能够使它动态更新,而无需完全重新加载该图。有办法做到这一点吗?如果是这样,怎么办?以下是我的意思的摘要:

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

2 个答案:

答案 0 :(得分:1)

这里的问题最终是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

答案 1 :(得分:0)

已经内置了一个用于在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