Let's make a diagram that can be clicked with IPython

I will show you how to make a diagram that can be clicked with IPython. The easiest way is to use the ʻinteract function (ʻIPython.html.widgets.interact) that automatically creates an interactive UI.

Load the required modules first.

from __future__ import print_function
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets

Then, first define the function you want to operate interactively.

def f(x):
    print(x)

Next, pass the function you want to operate as the first argument of the ʻinteract` function, and pass a similar argument (widget abbreviation) as a keyword argument. That's it. When you execute the code below, a slider will appear, which is fun to click.

interact(f, x=(-10, 10, 2));

InteractivePlot01.png

So, the top and bottom are completely synonymous. The top is how to pass a widget abbreviation, and the bottom is how to pass an instance of widget.

interact(f, x=widgets.IntSliderWidget(min=-10, max=10, step=2, value=0));

InteractivePlot01.png

If you want to set the initial value, use ʻinteract` as a decorator.

@interact(x=(-10, 10, 2))
def g(x=8):
    print(x)

InteractivePlot02.png

Typical widgets are as follows. The keyword argument of the ʻinteract` function is either the widget abbreviation on the left or the widget instance on the right.

Keyword arguments Widget
True or False CheckboxWidget
u'Hello World!' TextareaWidget
Using integersvalue or (min,max) or (min,max,step) IntSliderWidget
Using real numbersvalue or (min,max) or (min,max,step) FloatSliderWidget
('orange','apple') or {'one':1,'two':2} DropdownWidget

When combined with drawing, it will be even more fun to click. The figure below is a demo that dynamically draws $ y = A , sin (\ omega x + \ alpha) $. If you change the three parameters $ A \ in [0.1, 4.0], \ omega \ in [0.1, 4.0], \ alpha \ in [-\ pi, \ pi] $ with the slider, the figure will change accordingly. To do. This is fun!

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
@interact(amp=(0.1, 4.0, 0.1), omega=(0.1, 4.0, 0.1), phase=(-np.pi, np.pi, 0.1), 
          fn = {'sin': np.sin, 'cos': np.cos, 'tan': np.tan})
def h(amp=1.0, omega=1.0, phase=0.0, fn=np.sin):
    domain=[-np.pi, np.pi]
    x = np.linspace(domain[0], domain[1], 100)
    y  = amp * fn(omega * x + phase)
    plt.plot(x, y)
    plt.plot(-phase/omega, 0, 'or')
    plt.xlim(domain)
    plt.ylim([-4, 4])

InteractivePlot.png

Recommended Posts

Let's make a diagram that can be clicked with IPython
[Python] Make a graph that can be moved around with Plotly
Make a Spinbox that can be displayed in Binary with Tkinter
Make a currency chart that can be moved around with Plotly (2)
Make a Spinbox that can be displayed in HEX with Tkinter
Let's make a GUI with python.
Let's make a breakout with wxPython
Let's make a graph with python! !!
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
How to make a rock-paper-scissors bot that can be easily moved (commentary)
Create a web app that can be easily visualized with Plotly Dash
Draw a graph that can be moved around with HoloViews and Bokeh
File types that can be used with Go
List packages that can be updated with pip
Let's replace UWSC with Python (5) Let's make a Robot
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
A memo for making a figure that can be posted to a journal with matplotlib
Format DataFrame data with Pytorch into a form that can be trained with NN
I made a familiar function that can be used in statistics with Python
Make slides with iPython
Let's make a simple game with Python 3 and iPhone
Color list that can be set with tkinter (memorial)
Python knowledge notes that can be used with AtCoder
Let's make dependency management with pip a little easier
Let's make a Mac app with Tkinter and py2app
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
[Super easy] Let's make a LINE BOT with Python.
Limits that can be analyzed at once with MeCab
Convert images from FlyCapture SDK to a form that can be used with openCV
Let's make an app that can search similar images with Python and Flask Part2
I tried to make a memo app that can be pomodoro, but a reflection record
Let's make a websocket client with Python. (Access token authentication)
Format summary of formats that can be serialized with gensim
Let's make a Discord Bot.
It seems that Skeleton Tracking can be done with RealSense
NumPy zeros can be defined even with a size of 0
Let's create a script that registers with Ideone.com in Python.
Let's create a PRML diagram with Python, Numpy and matplotlib.
Let's make Othello with wxPython
Let's make a robot that solves the Rubik's Cube! 2 Algorithm
Let's make dice with tkinter
Make a fortune with Python
Let's make a robot that solves the Rubik's Cube! 1 Overview
Create a web API that can deliver images with Django
Let's make a rock-paper-scissors game
Implement a thread that can be paused by exploiting yield
I investigated the pretreatment that can be done with PyCaret
I made a plug-in that can "Daruma-san fell" with Minecraft
Make a fire with kdeplot
When developing with ipython, scrapy can no longer be read
You can try it with copy! Let's draw a cool network diagram with networkx of Python
Understand the probabilities and statistics that can be used for progress management with a python program
About the matter that torch summary can be really used when building a model with Pytorch
[Python] A program that finds the maximum number of toys that can be purchased with your money
Create a program that can generate your favorite images with Selenium