{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.9.0'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add Hover Text" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "data = [\n", " go.Scatter(\n", " x = [1,2,3,4,5],\n", " y = [2,1,6,4,4],\n", " text = [\"Text A\", \"Text B\", \"Text C\", \"Text D\", \"Text E\"],\n", " hoverinfo = 'text',\n", " marker = dict(\n", " color = 'green'\n", " ),\n", " showlegend = False\n", " )\n", "]\n", "\n", "py.iplot(data, filename = \"add-hover-text\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Format Hover Text" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "data = [\n", " go.Scatter(\n", " x = [1,2,3,4,5],\n", " y = [2.02825,1.63728,6.83839,4.8485,4.73463],\n", " hoverinfo = 'y',\n", " marker = dict(\n", " color = 'green'\n", " ),\n", " showlegend = False\n", " )\n", "]\n", "\n", "layout = go.Layout(\n", " title = \"Set hover text formatting
https://github.com/d3/d3-time-format/blob/master/README.md#locale_format\",\n", " titlefont = dict(\n", " size = 10\n", " ),\n", " xaxis = dict(\n", " zeroline = False\n", " ),\n", " yaxis = dict(\n", " hoverformat = '.2f'\n", " )\n", ")\n", "\n", "fig = go.Figure(data=data,layout=layout)\n", "py.iplot(fig, filename = \"format-hover-text\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Hovertemplate" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.io as pio\n", "\n", "\n", "data = [\n", " go.Scatter(\n", " x = [1,2,3,4,5],\n", " y = [2.02825,1.63728,6.83839,4.8485,4.73463],\n", " hovertemplate = 'Price: $%{y:.2f}'\n", " '
X: %{x}
'\n", " '%{text}',\n", " text = ['Custom text {}'.format(i + 1) for i in range(5)],\n", " showlegend = False\n", " ),\n", " go.Scatter(\n", " x = [1,2,3,4,5],\n", " y = [3.02825,2.63728,4.83839,3.8485,1.73463],\n", " hovertemplate = 'Price: %{y:$.2f}',\n", " showlegend = False\n", " )\n", "]\n", "\n", "layout = go.Layout(\n", " title = \"Set hover text with hovertemplate\",\n", " template = pio.templates['plotly'],\n", ")\n", "\n", "fig = go.Figure(data=data,layout=layout)\n", "py.iplot(fig, filename = \"hovertemplate-basic\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Advanced Hovertemplate" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.io as pio\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/job-automation-probability.csv')\n", "\n", "data = [\n", " dict(\n", " type = 'scatter',\n", " mode = 'markers',\n", " x = dff['prob'],\n", " y = dff['Average annual wage'],\n", " text = dff['short occupation'],\n", " name = education_level,\n", " marker = dict(size = dff['numbEmployed'], sizeref = 4000, sizemode = 'area'),\n", " hovertemplate = \"%{text}

\" +\n", " \"%{yaxis.title.text}: %{y:$,.0f}
\" +\n", " \"%{xaxis.title.text}: %{x:.0%}
\" +\n", " \"Number Employed: %{marker.size:,}\" +\n", " \"\"\n", " ) for dff, education_level in [(df[df.education == education_level], education_level) for education_level in df.education.unique()]\n", "]\n", "\n", "layout = go.Layout(\n", " title = \"Higher Risk of Job Automation in Lower Paying Jobs\",\n", " template = pio.templates['plotly'],\n", " legend = dict(orientation = 'h', y = -0.3),\n", " xaxis = dict(title = 'Automation Probability'),\n", " yaxis = dict(title = 'Income')\n", ")\n", "\n", "fig = dict(data=data,layout=layout)\n", "py.iplot(fig, filename = \"hovertemplate-advanced\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dash Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its [source code](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-hoverplot) can easily be deployed to a PaaS." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-hoverplot/\", width=\"100%\", height=\"920px\", frameBorder=\"0\")" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-hoverplot/code\", width=\"100%\", height=500, frameBorder=\"0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/ for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-6xhb4kuc\n", "Building wheels for collected packages: publisher\n", " Building wheel for publisher (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-xlgqz02g/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.13\n", " Uninstalling publisher-0.13:\n", " Successfully uninstalled publisher-0.13\n", "Successfully installed publisher-0.13\n", "\u001b[33mYou are using pip version 19.0.3, however version 19.1.1 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'hover-text-and-formatting.ipynb', 'python/hover-text-and-formatting/', 'Hover Text and Formatting',\n", " 'How to use hover text and formatting in Python with Plotly.',\n", " title = 'Hover Text and Formatting | Plotly',\n", " has_thumbnail='true', thumbnail='thumbnail/hover-text.png', \n", " language='python', \n", " display_as='file_settings', order=20, ipynb='~notebook_demo/198',\n", " uses_plotly_offline=False)" ] } ], "metadata": { "anaconda-cloud": {}, "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }