-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PanZoom Toolbar #316
Comments
Well, you can do this entirely on the python side using Does that make any sense? |
Basically, the concept of a |
Okay I think I got it. This is a working example. Maybe not pretty but does the job. Any suggestions? import numpy as np
from IPython.display import display
from bqplot import LinearScale, Axis, Lines, Figure, Toolbar, PanZoom
from ipywidgets import ToggleButton
x_label = ""
y_label = ""
title = ""
x_sc = LinearScale()
y_sc = LinearScale()
line = Lines(scales={'x':x_sc, 'y':y_sc})
ax_x = Axis(scale=x_sc,
label=x_label)
ax_y = Axis(scale=y_sc,
label=y_label, orientation='vertical')
# Zoom only y-scale
py = PanZoom(scales={'y': [y_sc]})
def py_update(new):
if tb.value:
fig.interaction = py
else:
fig.interaction = None
tb = ToggleButton(description="py")
tb.observe(py_update, "value")
fig = Figure(marks=[line], axes=[ax_x, ax_y],
title=title)
tb0 = Toolbar(figure=fig)
fig.marks[0].x = np.arange(10)
fig.marks[0].y = np.random.random(10)
display(fig, tb0, tb) Now the issue is, that the reste button wont work any more... so I have to add a new one, or? |
In ToggleButtons, you can use options and create your own reset button for now. But @SylvainCorlay for this purpose, shouldn't |
I had: import numpy as np
from IPython.display import display
from bqplot import LinearScale, Axis, Lines, Figure, Toolbar, PanZoom
from ipywidgets import ToggleButtons
x_label = ""
y_label = ""
title = ""
x_sc = LinearScale()
y_sc = LinearScale()
line = Lines(scales={'x':x_sc, 'y':y_sc})
ax_x = Axis(scale=x_sc,
label=x_label)
ax_y = Axis(scale=y_sc,
label=y_label, orientation='vertical')
def py_update(new):
if tb.value == 'y':
tb0._panzoom.scales={'y': [y_sc]}
elif tb.value == 'x':
tb0._panzoom.scales={'x': [x_sc]}
else:
tb0._panzoom.scales={'x': [x_sc], 'y': [y_sc]}
tb = ToggleButtons(options=['y', 'x', 'both'], value='both')
tb.observe(py_update, "value")
fig = Figure(marks=[line], axes=[ax_x, ax_y],
title=title)
tb0 = Toolbar(figure=fig)
fig.marks[0].x = np.arange(10)
fig.marks[0].y = np.random.random(10)
display(fig, tb0, tb) |
Ah that looks good. I give it a try. Thanks a lot. |
So I added a guard in the def py_update(new):
if not tb0._panning:
return
if tb.value == 'y':
tb0._panzoom.scales={'y': [y_sc]}
elif tb.value == 'x':
tb0._panzoom.scales={'x': [x_sc]}
else:
tb0._panzoom.scales={'x': [x_sc], 'y': [y_sc]} |
When you switch from |
I try this, but I don't know how. I don't find any property in the |
@SylvainCorlay Any answers? |
All, first of all, I think BQPlot and Ipywidgets are excellent. Here is my solution for x+y zoom. @deisi - it includes the reset of zoom. Latest version of ipwidgets 7.0b2 required to set the width of the togglebuttons pip install --pre bqplot from bqplot.interacts import PanZoom
import ipywidgets as widgets
import bqplot as bq
from traitlets import link
from collections import OrderedDict
import numpy as np
buttonWidth = '50px'
x_sc = bq.LinearScale()
y_sc = bq.LinearScale()
x_data = np.arange(500)
y_data = np.random.randn(3, 500)
line_chart = bq.Lines(x=x_data, y=y_data, scales= {'x': x_sc, 'y': y_sc},
display_legend=True, labels=["line 1", "line 2", "line 3"] )
ax_x = bq.Axis(scale=x_sc)
ax_y = bq.Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')
fig = bq.Figure(marks=[line_chart], axes=[ax_x, ax_y])
fig.layout.width = '95%'
pz = PanZoom(scales={'x': [x_sc], 'y': [y_sc]})
pzx = PanZoom(scales={'x': [x_sc]})
pzy = PanZoom(scales={'y': [y_sc], })
#
zoom_interacts = widgets.ToggleButtons(
options=OrderedDict([
('xy ', pz),
('x ', pzx),
('y ', pzy),
(' ', None)]),
icons = ["arrows", "arrows-h", "arrows-v", "stop"],
tooltips = ["zoom/pan in x & y", "zoom/pan in x only", "zoom/pan in y only", "cancel zoom/pan"]
)
zoom_interacts.style.button_width = buttonWidth
ResetZoomButton = widgets.Button(
description='',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Reset zoom',
icon='arrows-alt'
)
def resetZoom(new):
# Reset the x and y axes on the figure
fig.axes[0].scale.min = None
fig.axes[1].scale.min = None
fig.axes[0].scale.max = None
fig.axes[1].scale.max = None
ResetZoomButton.on_click(resetZoom)
ResetZoomButton.layout.width = buttonWidth
link((zoom_interacts, 'value'), (fig, 'interaction'))
widgets.VBox([fig, widgets.HBox([zoom_interacts,ResetZoomButton])], align_self='stretch') |
Hello
I'm trying to create a custom toolbar for my needs, that allows me to Zoom in x and y axis direction independently. I'm aware of https://github.com/bloomberg/bqplot/issues/121, but I absolutely don't understand how to use PanZoom and how to incorporate it into a custom Toolbar. As it is suggested there. Could somebody clarify this for me.
Thanks a lot.
The text was updated successfully, but these errors were encountered: