{ "metadata": { "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" }, "kernelspec": { "name": "python", "display_name": "Pyolite", "language": "python" } }, "nbformat_minor": 4, "nbformat": 4, "cells": [ { "cell_type": "markdown", "source": "# Plotly in JupyterLite\n\n`plotly.py` is an interactive, open-source, and browser-based graphing library for Python: https://plotly.com/python/", "metadata": {} }, { "cell_type": "markdown", "source": "## Install the dependencies", "metadata": {} }, { "cell_type": "code", "source": "import micropip\nawait micropip.install('plotly')\n\n# set the default renderer\nimport os\nos.environ[\"PLOTLY_RENDERER\"] = \"jupyterlab\"", "metadata": { "trusted": true }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": "## Basic Figure", "metadata": { "tags": [] } }, { "cell_type": "code", "source": "import plotly.graph_objects as go\nfig = go.Figure()\nfig.add_trace(go.Scatter(y=[2, 1, 4, 3]))\nfig.add_trace(go.Bar(y=[1, 4, 3, 2]))\nfig.update_layout(title = 'Hello Figure')\nfig.show()", "metadata": { "trusted": true }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": "## Basic Table with a Pandas DataFrame", "metadata": { "tags": [] } }, { "cell_type": "code", "source": "import plotly.graph_objects as go\nimport pandas as pd\n\nfrom js import fetch\n\nURL = \"https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv\"\n\nres = await fetch(URL)\ntext = await res.text()\n\nfilename = 'data.csv'\n\nwith open(filename, 'w') as f:\n f.write(text)\n\ndf = pd.read_csv(filename)\n\nfig = go.Figure(data=[go.Table(\n header=dict(values=list(df.columns),\n fill_color='paleturquoise',\n align='left'),\n cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],\n fill_color='lavender',\n align='left'))\n])\n\nfig.show()", "metadata": { "trusted": true }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": "## Quiver Plot with Points", "metadata": {} }, { "cell_type": "code", "source": "import plotly.figure_factory as ff\nimport plotly.graph_objects as go\n\nimport numpy as np\n\nx,y = np.meshgrid(np.arange(-2, 2, .2),\n np.arange(-2, 2, .25))\nz = x*np.exp(-x**2 - y**2)\nv, u = np.gradient(z, .2, .2)\n\n# Create quiver figure\nfig = ff.create_quiver(x, y, u, v,\n scale=.25,\n arrow_scale=.4,\n name='quiver',\n line_width=1)\n\n# Add points to figure\nfig.add_trace(go.Scatter(x=[-.7, .75], y=[0,0],\n mode='markers',\n marker_size=12,\n name='points'))\n\nfig.show()", "metadata": { "trusted": true }, "execution_count": null, "outputs": [] } ] }