{ "cells": [ { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact, fixed\n", "import ipywidgets as widgets\n", "from bqplot import pyplot as plt\n", "import numpy as np\n", "from numpy.random import rand" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "90ec287130a64a4fa0f7efbfaceb036f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=10.0, description='x', max=30.0, min=-10.0), Output()), _dom_classes=(…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def f(x):\n", " return x**2\n", "interact(f, x=10.);" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5ece1d3b386045489fefae8efd2f5b63", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FloatSlider(value=1.0, description='x', max=30.0, min=-10.0)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b4f6f3248af54ea09ac7e3e8dc42caa3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mon_slider = widgets.FloatSlider(\n", " value=1,\n", " min=-10,\n", " max=30,\n", " step=0.1,\n", " description='x',\n", " disabled=False,\n", " continuous_update=True,\n", " orientation='horizontal',\n", " readout = True\n", ")\n", "\n", "resultat = widgets.Output()\n", "\n", "def maj_resultat(change):\n", " with resultat:\n", " resultat.clear_output()\n", " print(f(change['new']))\n", "\n", "mon_slider.observe(maj_resultat, names='value')\n", "\n", "display(mon_slider,resultat)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "82492b6cdc134af8944680e8285ff509", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='fonction', options=('parabole', 'sinus', 'hasard'), value='parabol…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4d3e4e1174eb472fa895832261e57517", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(Figure(animation_duration=300, axes=[Axis(label='Axe des x', scale=LinearScale()), Axis(label='…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipywidgets import interact, fixed\n", "import ipywidgets as widgets\n", "from bqplot import pyplot as plt\n", "import numpy as np\n", "from numpy.random import rand\n", "\n", "#génère des abscisses\n", "x = np.arange(0,10,0.1)\n", "\n", "#trace une figure y = f(x)\n", "ma_figure = plt.figure(animation_duration = 300)\n", "mon_tracé = plt.scatter(x, x**2, enable_move=True)\n", "plt.xlabel('Axe des x')\n", "\n", "resultat2 = widgets.Output()\n", "\n", "#choix de la fonction à tracer\n", "@interact(fonction=['parabole', 'sinus', 'hasard'], x=fixed(x))\n", "def choix_fonction(fonction, x):\n", " if fonction=='parabole':\n", " with mon_tracé.hold_sync():\n", " mon_tracé.x = x\n", " mon_tracé.y = x**2\n", " plt.ylabel('x au carré')\n", " if fonction=='sinus':\n", " with mon_tracé.hold_sync():\n", " mon_tracé.x = x\n", " mon_tracé.y = np.sin(x)\n", " plt.ylabel('sin(x)')\n", " if fonction=='hasard':\n", " with mon_tracé.hold_sync():\n", " mon_tracé.x = x\n", " mon_tracé.y = rand(len(x))\n", " plt.ylabel('Nombres aléatoires')\n", "\n", "#fonction qui lit et affiche les coordonnées d'un point déplacé\n", "def affiche(name, value):\n", " with resultat2:\n", " resultat2.clear_output()\n", " print('Le point n° %i a été déplacé en x = %f y = %f'\n", " %(value['index'], value['point']['x'],value['point']['y']))\n", "#détecte le déplacement d'un point\n", "mon_tracé.on_drag_end(affiche) \n", "\n", "#créé la gui\n", "widgets.VBox([ma_figure,resultat2]) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }