{ "cells": [ { "cell_type": "markdown", "id": "8bf0c3d7", "metadata": {}, "source": [ "# Fit\n", "\n", "This example uses the `draw()` and `tween()` animations to animate a `scatter`, `plot`, `fill_between` and `text` plot objects.\n", "\n", "We first create our timeline and name our axes:" ] }, { "cell_type": "code", "execution_count": null, "id": "b8ecc773", "metadata": {}, "outputs": [], "source": [ "import diplotocus as dpl\n", "import numpy as np\n", "\n", "tl = dpl.Timeline(xlim=(0,1),ylim=(0,1.5))\n", "tl.main_axis.set_xlabel('x')\n", "tl.main_axis.set_ylabel('y')" ] }, { "cell_type": "markdown", "id": "feb8c9bf", "metadata": {}, "source": [ "Then, we create the data we want to plot (data points, the fitted relation and its uncertainty):" ] }, { "cell_type": "code", "execution_count": 11, "id": "825677b0", "metadata": {}, "outputs": [], "source": [ "x = np.random.uniform(0,1,100)\n", "y = np.random.normal(0.5*(1+x),0.2)\n", "\n", "popt, cov = np.polyfit(x,y,1,cov=True)\n", "err = np.sqrt(np.diag(cov))\n", "\n", "#Cheap & dirty way to get confidence bounds with numpy\n", "p_rand = np.random.normal(size=(10_000,2))*err + popt\n", "_x = np.linspace(0,1,1_000)\n", "_y = popt[0]*_x + popt[1]\n", "_ys = []\n", "for _p in p_rand:\n", " _ys.append(_p[0]*_x + _p[1])\n", "y_max = np.max(_ys,axis=0)\n", "y_min = np.min(_ys,axis=0)" ] }, { "cell_type": "markdown", "id": "3c4ade42", "metadata": {}, "source": [ "And finally, we animate it and render the video. To animate the confidence bounds growing, we start the `fill_between`'s `y1` and `y2` properties from the same y values as the line, and we `tween` them to `y_min` and `y_max`, the actual size of the $\\pm 1\\sigma$ bounds." ] }, { "cell_type": "code", "execution_count": null, "id": "9a0b36c9", "metadata": {}, "outputs": [], "source": [ "e = dpl.easeInOutCubic()\n", "s = dpl.scatter(x=x,y=y)\n", "p = dpl.plot(x=_x,y=_y,c='k',lw=3)\n", "t = dpl.text(x=0.05,y=1.2,string=f'f(x) = {popt[0]:.2f}x + {popt[1]:.2f}',fontsize=14)\n", "f = dpl.fill_between(x=_x,y1=_y,y2=_y,alpha=0.2,fc='green')\n", "\n", "s.draw(duration=60,sort='x',easing=e)\n", "p.draw(duration=60,delay=60,easing=e)\n", "t.draw(duration=60,delay=60,easing=e)\n", "f.tween(property='y1',start=_y,end=y_min,duration=60,delay=120,easing=e)\n", "f.tween(property='y2',start=_y,end=y_max,duration=60,delay=120,easing=e)\n", "\n", "tl.animate((s,p,t,f))\n", "tl.wait(duration=60)\n", "\n", "tl.save_video('../../_static/examples/fit.mp4')" ] }, { "cell_type": "code", "execution_count": 13, "id": "f5077e4e", "metadata": { "tags": [ "remove-input" ] }, "outputs": [ { "data": { "text/html": [ "\n", " \n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import HTML, display\n", "display(HTML(\"\"\"\n", " \n", "\"\"\"))" ] } ], "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.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }