Interactive plots in Jupyter (IPython) notebook with draggable points that call Python code when dragged

David Ketcheson

I'd like to make some interactive plots in the Jupyter notebook, in which certain points in the plot can be dragged by the user. The locations of those points should then be used as input to a Python function (in the notebook) that updates the plot.

Something like this has been accomplished here:

http://nbviewer.ipython.org/github/maojrs/ipynotebooks/blob/master/interactive_test.ipynb

but the callbacks are to Javascript functions. In some cases, the code that updates the plot needs to be extremely complex and would take a very long time to rewrite in Javascript. I'm willing to designate the draggable points in Javascript if necessary, but is it possible to call back to Python for updating the plot?

I'm wondering if tools like Bokeh or Plotly could provide this functionality.

Drew

Have you tried bqplot? The Scatter has an enable_move parameter, that when you set to True they allow points to be dragged. Furthermore, when you drag you can observe a change in the x or y value of the Scatter or Label and trigger a python function through that, which in turn generates a new plot. They do this in the Introduction notebook.

Jupyter notebook code:

# Let's begin by importing some libraries we'll need
import numpy as np
from __future__ import print_function # So that this notebook becomes both Python 2 and Python 3 compatible

# And creating some random data
size = 10
np.random.seed(0)
x_data = np.arange(size)
y_data = np.cumsum(np.random.randn(size)  * 100.0)

from bqplot import pyplot as plt

# Creating a new Figure and setting it's title
plt.figure(title='My Second Chart')
# Let's assign the scatter plot to a variable
scatter_plot = plt.scatter(x_data, y_data)

# Let's show the plot
plt.show()

# then enable modification and attach a callback function:

def foo(change):
    print('This is a trait change. Foo was called by the fact that we moved the Scatter')
    print('In fact, the Scatter plot sent us all the new data: ')
    print('To access the data, try modifying the function and printing the data variable')
    global pdata 
    pdata = [scatter_plot.x,scatter_plot.y]

# First, we hook up our function `foo` to the colors attribute (or Trait) of the scatter plot
scatter_plot.observe(foo, ['y','x'])

scatter_plot.enable_move = True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

iPython/Jupyter Notebook: How to Embed Interactive Graph Using Desmos API?

From Dev

Displaying rotatable 3D plots in IPython or Jupyter Notebook

From Dev

How to activate the code assistance in IPython Notebook / Jupyter

From Dev

Why is Jupyter Notebook creating duplicate plots when making updating plots

From Dev

Anaconda: Python 3 and 2 in IPython/Jupyter Notebook

From Dev

Jupyter (ipython notebook), hide menu and some toolbar buttons, or run python code on loading

From Dev

ggvis in jupyter/ipython notebook

From Dev

ipython/Jupyter notebook with authentication

From Dev

Is it possible to speed up interactive IPython Notebook plots by not generating new figures every time?

From Dev

Open 'ipython notebook' as: IPython notebook vs Jupyter

From Dev

Ipython Notebook: Default Initialization Python Code

From Dev

Jupyter Notebook: interactive plot with widgets

From Dev

jupyter notebook inline plots as svg

From Dev

Hide Code when exporting Jupyter notebook to HTML

From Dev

Draggable is not fit into droppable when dragged back

From Dev

autoupdate module in IPython / jupyter notebook

From Dev

Jupyter (IPython) notebook: Convert an HTML notebook to ipynb

From Dev

ipython notebook command in anaconda opens jupyter notebook?

From Dev

ipython-notebook: print rmagic plots as svg

From Dev

set IPython Notebook inline plots background not transparent

From Dev

How to flush plots in IPython Notebook sequentially?

From Dev

ipython-notebook: print rmagic plots as svg

From Dev

Interactive Jupyter/IPython notebooks in slideshow mode?

From Java

How to make inline plots in Jupyter Notebook larger?

From Dev

Automate standard jupyter/ipython notebook imports

From Dev

Default template for iPython notebook (using Jupyter)

From Dev

How to type in norm (latex) in ipython notebook (Jupyter)?

From Dev

Export individual cell in IPython/Jupyter notebook

From Dev

IPython/Jupyter notebook shortcut not working on Mac

Related Related

  1. 1

    iPython/Jupyter Notebook: How to Embed Interactive Graph Using Desmos API?

  2. 2

    Displaying rotatable 3D plots in IPython or Jupyter Notebook

  3. 3

    How to activate the code assistance in IPython Notebook / Jupyter

  4. 4

    Why is Jupyter Notebook creating duplicate plots when making updating plots

  5. 5

    Anaconda: Python 3 and 2 in IPython/Jupyter Notebook

  6. 6

    Jupyter (ipython notebook), hide menu and some toolbar buttons, or run python code on loading

  7. 7

    ggvis in jupyter/ipython notebook

  8. 8

    ipython/Jupyter notebook with authentication

  9. 9

    Is it possible to speed up interactive IPython Notebook plots by not generating new figures every time?

  10. 10

    Open 'ipython notebook' as: IPython notebook vs Jupyter

  11. 11

    Ipython Notebook: Default Initialization Python Code

  12. 12

    Jupyter Notebook: interactive plot with widgets

  13. 13

    jupyter notebook inline plots as svg

  14. 14

    Hide Code when exporting Jupyter notebook to HTML

  15. 15

    Draggable is not fit into droppable when dragged back

  16. 16

    autoupdate module in IPython / jupyter notebook

  17. 17

    Jupyter (IPython) notebook: Convert an HTML notebook to ipynb

  18. 18

    ipython notebook command in anaconda opens jupyter notebook?

  19. 19

    ipython-notebook: print rmagic plots as svg

  20. 20

    set IPython Notebook inline plots background not transparent

  21. 21

    How to flush plots in IPython Notebook sequentially?

  22. 22

    ipython-notebook: print rmagic plots as svg

  23. 23

    Interactive Jupyter/IPython notebooks in slideshow mode?

  24. 24

    How to make inline plots in Jupyter Notebook larger?

  25. 25

    Automate standard jupyter/ipython notebook imports

  26. 26

    Default template for iPython notebook (using Jupyter)

  27. 27

    How to type in norm (latex) in ipython notebook (Jupyter)?

  28. 28

    Export individual cell in IPython/Jupyter notebook

  29. 29

    IPython/Jupyter notebook shortcut not working on Mac

HotTag

Archive