{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction plotly\n", "\n", "##### Germain Salvato-Vallverdu [germain.vallverdu@univ-pau.fr](mailto:germain.vallverdu@univ-pau.fr)\n", "\n", "Ce notebook est une **très courte** introduction à plotly. Quelques liens utiles :\n", "\n", "* [Utilisation de plotly avec python](https://plot.ly/python/getting-started/)\n", "* [Doc python de référence](https://plot.ly/python/reference/)\n", "* [Utilisation de plotly avec R](https://plot.ly/r/)\n", "* [La galerie pour trouver des exemples](https://plot.ly/python/)\n", "\n", "**Important :** Par défaut plotly communique avec ses serveurs pour proposer des\n", "sercices collaboratifs et d'intégrations web. Pour éviter, si besoin, se passage\n", "par les serveurs plotly, il faudra penser à utiliser le mode `offline`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Chargement de plotly" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly\n", "import plotly.graph_objs as go\n", "import numpy as np\n", "# mode offline\n", "plotly.offline.init_notebook_mode()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Définition des fonctions\n", "\n", "On va tracer la fonction $S(t)$ et son envelope :\n", "\n", "$$\n", " S(t) = \\sin(2\\,t) \\,e^{-0.04\\, t}\n", "$$" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def signal(t):\n", " return np.sin(2 * t) * np.exp(-4e-2 * t)\n", "def envelope(t):\n", " return np.exp(-4e-2 * t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Un premier graphique\n", "\n", "* Chaque courbe est une `trace`\n", "* Une figure contient des données `Data` et un `Layout`\n", "* Les `Data` sont des listes de `trace`\n", "* L'ensemble des objets de plotly fonctionne avec une syntaxe `clef=valeur`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = np.linspace(0, 80, 200)\n", "trace = go.Scatter(\n", " x = t,\n", " y = signal(t),\n", ")\n", "data = go.Data([trace])\n", "plotly.offline.iplot(data)\n", "plotly.plotly.image.save_as(data, \"/Users/gvallver/git/python_sciences/plotly/graph1.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### On ajoute l'envelope\n", "\n", "Il faut ajouter des traces :" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = np.linspace(0, 80, 200)\n", "trace = go.Scatter(\n", " x = t,\n", " y = signal(t)\n", ")\n", "envp = go.Scatter(\n", " x = t,\n", " y = envelope(t),\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "envm = go.Scatter(\n", " x = t,\n", " y = -envelope(t),\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "data = go.Data([trace, envp, envm])\n", "plotly.offline.iplot(data)\n", "plotly.plotly.image.save_as(data, \"/Users/gvallver/git/python_sciences/plotly/graph2.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### On ajoute des informations\n", "\n", "On ajoute un layout." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = np.linspace(0, 80, 200)\n", "trace = go.Scatter(\n", " x = t,\n", " y = signal(t)\n", ")\n", "envp = go.Scatter(\n", " x = t,\n", " y = envelope(t),\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "envm = go.Scatter(\n", " x = t,\n", " y = -envelope(t),\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "# on regroupe les traces dans l'objet Data\n", "data = go.Data([trace, envp, envm])\n", "# on s'occupe de la mise en forme globale via le Layout\n", "layout = go.Layout(\n", " title = \"Ma super fonction amortie\",\n", " xaxis = go.XAxis(\n", " title = \"temps (s)\"\n", " ),\n", " yaxis = go.YAxis(\n", " title = \"Signal (ua)\"\n", " )\n", ")\n", "# la figure est un objet Data + un objet Layout\n", "figure = go.Figure(data=data, layout=layout)\n", "plotly.offline.iplot(figure)\n", "plotly.plotly.image.save_as(figure, \"/Users/gvallver/git/python_sciences/plotly/graph3.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Et la légende ?\n", "\n", "Il suffit de donner les noms, vous avez du remarquer que la légende est déjà présente par défaut." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = np.linspace(0, 80, 200)\n", "trace = go.Scatter(\n", " x = t,\n", " y = signal(t),\n", " name = \"signal\" # nom de la trace\n", ")\n", "envp = go.Scatter(\n", " x = t,\n", " y = envelope(t),\n", " name = \"envelope\", # nom de la trace\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "envm = go.Scatter(\n", " x = t,\n", " y = -envelope(t),\n", " showlegend = False,\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "data = go.Data([trace, envp, envm])\n", "layout = go.Layout(\n", " title = \"Ma super fonction amortie\",\n", " xaxis = go.XAxis(\n", " title = \"temps (s)\"\n", " ),\n", " yaxis = go.YAxis(\n", " title = \"Signal (ua)\"\n", " )\n", ")\n", "figure = go.Figure(data=data, layout=layout)\n", "plotly.offline.iplot(figure)\n", "plotly.plotly.image.save_as(figure, \"/Users/gvallver/git/python_sciences/plotly/graph4.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Échelle des axes\n", "\n", "L'échelle des axes est définie dans le Layout via les objets XAxis et YAxis. Dans plotly chaque élément du graphique a un objet qui lui est propre qui se trouve dans `plotly.graphical_objs`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = np.linspace(0, 80, 200)\n", "trace = go.Scatter(\n", " x = t,\n", " y = signal(t),\n", " name = \"signal\"\n", ")\n", "envp = go.Scatter(\n", " x = t,\n", " y = envelope(t),\n", " name = \"envelope\",\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "envm = go.Scatter(\n", " x = t,\n", " y = -envelope(t),\n", " showlegend = False,\n", " line = go.Line(\n", " color = \"red\",\n", " dash = \"dash\"\n", " )\n", ")\n", "data = go.Data([trace, envp, envm])\n", "layout = go.Layout(\n", " title = \"Ma super fonction amortie\",\n", " xaxis = go.XAxis(\n", " title = \"temps (s)\",\n", " range = [0, 40]\n", " ),\n", " yaxis = go.YAxis(\n", " title = \"Signal (ua)\",\n", " range = [-1.2, 1.5]\n", " )\n", ")\n", "figure = go.Figure(data=data, layout=layout)\n", "plotly.offline.iplot(figure)\n", "plotly.plotly.image.save_as(figure, \"/Users/gvallver/git/python_sciences/plotly/graph5.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion\n", "\n", "En conclusion : à vous de jouer et comme pour matplotlib, rendez-vous sur la\n", "[la galerie](https://plot.ly/python/)." ] } ], "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.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }