{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from ipywidgets import interact, FloatSlider" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.10204082, 0.20408163, 0.30612245, 0.40816327,\n", " 0.51020408, 0.6122449 , 0.71428571, 0.81632653, 0.91836735,\n", " 1.02040816, 1.12244898, 1.2244898 , 1.32653061, 1.42857143,\n", " 1.53061224, 1.63265306, 1.73469388, 1.83673469, 1.93877551,\n", " 2.04081633, 2.14285714, 2.24489796, 2.34693878, 2.44897959,\n", " 2.55102041, 2.65306122, 2.75510204, 2.85714286, 2.95918367,\n", " 3.06122449, 3.16326531, 3.26530612, 3.36734694, 3.46938776,\n", " 3.57142857, 3.67346939, 3.7755102 , 3.87755102, 3.97959184,\n", " 4.08163265, 4.18367347, 4.28571429, 4.3877551 , 4.48979592,\n", " 4.59183673, 4.69387755, 4.79591837, 4.89795918, 5. ])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = np.linspace(0.0, 5.0)\n", "t" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "A = 0.5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.05093191, 0.10133397, 0.15068182, 0.19846207,\n", " 0.24417767, 0.28735302, 0.32753895, 0.36431739, 0.39730573,\n", " 0.42616078, 0.45058236, 0.47031639, 0.48515758, 0.49495154,\n", " 0.49959636, 0.49904374, 0.49329942, 0.48242315, 0.4665281 ,\n", " 0.44577962, 0.42039355, 0.39063401, 0.35681059, 0.31927516,\n", " 0.27841822, 0.23466481, 0.18847011, 0.1403147 , 0.09069956,\n", " 0.04014084, -0.01083548, -0.06169907, -0.11192079, -0.16097816,\n", " -0.20836083, -0.25357585, -0.29615286, -0.3356489 , -0.37165308,\n", " -0.40379085, -0.43172786, -0.45517347, -0.47388378, -0.48766414,\n", " -0.49637119, -0.49991433, -0.49825672, -0.4914156 , -0.47946214])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = A*np.sin(t)\n", "y" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "%matplotlib widget" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4db118598c584376851bb4fde3f2ceda", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig, ax = plt.subplots()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "lines = ax.plot(t, y)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lines" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(lines)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = lines[0]\n", "line" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def update_line(A):\n", " y = A*np.sin(t)\n", " return y" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.20372766, 0.40533587, 0.60272726, 0.7938483 ,\n", " 0.9767107 , 1.14941208, 1.31015579, 1.45726957, 1.58922293,\n", " 1.70464314, 1.80232944, 1.88126557, 1.94063034, 1.97980615,\n", " 1.99838546, 1.99617496, 1.97319767, 1.92969262, 1.8661124 ,\n", " 1.78311846, 1.68157421, 1.56253605, 1.42724236, 1.27710064,\n", " 1.11367287, 0.93865923, 0.75388043, 0.5612588 , 0.36279822,\n", " 0.16056335, -0.04334191, -0.24679627, -0.44768314, -0.64391263,\n", " -0.8334433 , -1.01430342, -1.18461144, -1.34259559, -1.48661232,\n", " -1.61516338, -1.72691142, -1.82069389, -1.89553514, -1.95065657,\n", " -1.98548475, -1.99965734, -1.9930269 , -1.96566241, -1.91784855])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "update_line(2.0)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def update_line(A=0.5):\n", " y = A*np.sin(t)\n", " return y" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.10186383, 0.20266794, 0.30136363, 0.39692415,\n", " 0.48835535, 0.57470604, 0.6550779 , 0.72863478, 0.79461147,\n", " 0.85232157, 0.90116472, 0.94063279, 0.97031517, 0.98990308,\n", " 0.99919273, 0.99808748, 0.98659884, 0.96484631, 0.9330562 ,\n", " 0.89155923, 0.84078711, 0.78126802, 0.71362118, 0.63855032,\n", " 0.55683643, 0.46932961, 0.37694022, 0.2806294 , 0.18139911,\n", " 0.08028167, -0.02167096, -0.12339814, -0.22384157, -0.32195632,\n", " -0.41672165, -0.50715171, -0.59230572, -0.67129779, -0.74330616,\n", " -0.80758169, -0.86345571, -0.91034694, -0.94776757, -0.97532829,\n", " -0.99274237, -0.99982867, -0.99651345, -0.9828312 , -0.95892427])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "update_line(A=1.0)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.05093191, 0.10133397, 0.15068182, 0.19846207,\n", " 0.24417767, 0.28735302, 0.32753895, 0.36431739, 0.39730573,\n", " 0.42616078, 0.45058236, 0.47031639, 0.48515758, 0.49495154,\n", " 0.49959636, 0.49904374, 0.49329942, 0.48242315, 0.4665281 ,\n", " 0.44577962, 0.42039355, 0.39063401, 0.35681059, 0.31927516,\n", " 0.27841822, 0.23466481, 0.18847011, 0.1403147 , 0.09069956,\n", " 0.04014084, -0.01083548, -0.06169907, -0.11192079, -0.16097816,\n", " -0.20836083, -0.25357585, -0.29615286, -0.3356489 , -0.37165308,\n", " -0.40379085, -0.43172786, -0.45517347, -0.47388378, -0.48766414,\n", " -0.49637119, -0.49991433, -0.49825672, -0.4914156 , -0.47946214])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "update_line()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def update_line(A=0.5):\n", " y = A*np.sin(t)\n", " line.set_ydata(y)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "update_line(A=0.2)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "473258a7d18349c6af2376551774fb7e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=0.5, description='A', max=5.0), Output()), _dom_classes=('widget-inter…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "widget = interact(update_line, A=FloatSlider(value=0.5, min=0.0, max=5.0))" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 4 }