在bqplot中动态调整轴

问题描述 投票:3回答:2

我正在开发一个基于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
jupyter-notebook jupyter bqplot
2个回答
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


0
投票

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

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

您需要添加额外的行来构建交互。你可以在字典中传递一两个音阶,我在这里将它限制为x。然后在创建图时使用交互kwarg传递PanZoom对象。

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
© www.soinside.com 2019 - 2024. All rights reserved.