{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "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": [ "#### NYC Flights Database" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import datetime\n", "import numpy as np\n", "import pandas as pd\n", "import plotly.graph_objs as go\n", "import plotly.plotly as py\n", "\n", "from ipywidgets import widgets \n", "from IPython.display import display, clear_output, Image\n", "from plotly.widgets import GraphWidget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll be making an application to take a look at delays from all flights out of NYC in the year 2013." ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/yankev/testing/master/datasets/nycflights.csv')\n", "df = df.drop(df.columns[[0]], axis=1) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's get the set of all the `airlines`, so that we can type the right things into the search box later. " ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['UA', 'AA', 'B6', 'DL', 'EV', 'MQ', 'US', 'WN', 'VX', 'FL', 'AS',\n", " '9E', 'F9', 'HA', 'YV', 'OO'], dtype=object)" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['carrier'].unique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's assign the widgets that we're going to be using in our app. In general all these widgets will be used to filter the data set, and thus what we visualize." ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [], "source": [ "month = widgets.FloatSlider(\n", " value=1.0,\n", " min=1.0,\n", " max=12.0,\n", " step=1.0,\n", " description='Month:',\n", " continuous_update = False\n", ")\n", "\n", "use_date = widgets.Checkbox(\n", " description='Date: ',\n", " value=True,\n", ")\n", "\n", "container = widgets.HBox(children=[use_date, month] )\n", "\n", "textbox = widgets.Text(\n", " description='Airline: ',\n", " value='Delta',\n", ")\n", "\n", "origin = widgets.Dropdown(\n", " options=list(df['origin'].unique()),\n", " value='LGA',\n", " description='Origin Airport:',\n", ")\n", "\n", "\n", "### Assign an emptry graph widget with two traces\n", "\n", "g = GraphWidget('https://plotly.com/~kevintest/1192/')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let now write a function that will handle the input from the widgets, and alter the state of the graph." ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "def validate():\n", " # print \"validating\"\n", " if origin.value in df['origin'].unique() and textbox.value in df['carrier'].unique():\n", " return True\n", " else:\n", " return False\n", "\n", "def response(change):\n", " if validate():\n", " # print \"using date\"\n", " if use_date.value:\n", " filter_list = [i and j and k for i,j,k in \n", " zip(df['month'] == month.value, df['carrier'] == textbox.value, \n", " df['origin'] == origin.value)]\n", " temp_df = df[filter_list]\n", " x1 = temp_df['arr_delay']\n", " x2 = temp_df['dep_delay']\n", " g.restyle({'x': [x1], 'name': 'Arrival Delays', 'type': 'histogram', \n", " 'opacity': 0.75}, indices=0) \n", " g.restyle({'x': [x2], 'name': 'Departure Delays', 'type': 'histogram', \n", " 'opacity': 0.75}, indices=1)\n", " g.relayout({'barmode': 'overlay', 'xaxis': {'title': 'Delay in Minutes'},\n", " 'yaxis': {'title': 'Number of Delays'}})\n", " # print temp_df.head()\n", " \n", " else:\n", " # print \"not using date\"\n", " filter_list = [i and j for i,j in \n", " zip(df['carrier'] == 'DL', df['origin'] == origin.value)]\n", " temp_df = df[filter_list]\n", " x1 = temp_df['arr_delay']\n", " x2 = temp_df['dep_delay']\n", " g.restyle({'x': [x1], 'name': 'Arrival Delays', 'type': 'histogram', \n", " 'opacity': 0.75}, indices=0) \n", " g.restyle({'x': [x2], 'name': 'Departure Delays', 'type': 'histogram', \n", " 'opacity': 0.75}, indices=1)\n", " g.relayout({'barmode': 'overlay'})\n", " # print temp_df.head()\n", " \n", "\n", "origin.observe(response, names=\"value\")\n", "textbox.observe(response, names=\"value\")\n", "month.observe(response, names=\"value\")\n", "use_date.observe(response, names=\"value\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Time to try the app out!!" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "display(container)\n", "container2 = widgets.HBox([origin, textbox])\n", "display(container2)\n", "display(g)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class GraphWidget in module plotly.widgets.graph_widget:\n", "\n", "class GraphWidget(ipywidgets.widgets.widget.DOMWidget)\n", " | An interactive Plotly graph widget for use in IPython\n", " | Notebooks.\n", " | \n", " | Method resolution order:\n", " | GraphWidget\n", " | ipywidgets.widgets.widget.DOMWidget\n", " | ipywidgets.widgets.widget.Widget\n", " | traitlets.config.configurable.LoggingConfigurable\n", " | traitlets.config.configurable.Configurable\n", " | traitlets.traitlets.HasTraits\n", " | traitlets.traitlets._NewBase\n", " | traitlets.traitlets.HasDescriptors\n", " | traitlets.traitlets._NewBase\n", " | __builtin__.object\n", " | \n", " | Methods defined here:\n", " | \n", " | __init__(self, graph_url='https://plotly.com/~playground/7', **kwargs)\n", " | Initialize a plotly graph widget\n", " | \n", " | Args:\n", " | graph_url: The url of a Plotly graph\n", " | \n", " | Example:\n", " | ```\n", " | GraphWidget('https://plotly.com/~chris/3375')\n", " | ```\n", " | \n", " | add_traces(self, traces, new_indices=None)\n", " | Add new data traces to a graph.\n", " | \n", " | If `new_indices` isn't specified, they are simply appended.\n", " | \n", " | Args:\n", " | traces (dict or list of dicts, or class of plotly.graph_objs):trace\n", " | new_indices (list[int]|None), optional: The final indices the\n", " | added traces should occupy in the graph.\n", " | \n", " | Examples:\n", " | Initialization - Start each example below with this setup:\n", " | ```\n", " | from plotly.widgets import GraphWidget\n", " | from plotly.graph_objs import Scatter\n", " | from IPython.display import display\n", " | \n", " | graph = GraphWidget('https://plotly.com/~chris/3979')\n", " | display(graph)\n", " | ```\n", " | \n", " | Example 1 - Add a scatter/line trace to the graph\n", " | ```\n", " | graph.add_traces(Scatter(x = [1, 2, 3], y = [5, 4, 5]))\n", " | ```\n", " | \n", " | Example 2 - Add a scatter trace and set it to to be the\n", " | second trace. This will appear as the second\n", " | item in the legend.\n", " | ```\n", " | graph.add_traces(Scatter(x = [1, 2, 3], y = [5, 6, 5]),\n", " | new_indices=[1])\n", " | ```\n", " | \n", " | Example 3 - Add multiple traces to the graph\n", " | ```\n", " | graph.add_traces([\n", " | Scatter(x = [1, 2, 3], y = [5, 6, 5]),\n", " | Scatter(x = [1, 2.5, 3], y = [5, 8, 5])\n", " | ])\n", " | ```\n", " | \n", " | delete_traces(self, indices)\n", " | Delete data traces from a graph.\n", " | \n", " | Args:\n", " | indices (list[int]): The indices of the traces to be removed\n", " | \n", " | Example - Delete the 2nd trace:\n", " | ```\n", " | from plotly.widgets import GraphWidget\n", " | from IPython.display import display\n", " | \n", " | graph = GraphWidget('https://plotly.com/~chris/3979')\n", " | display(graph)\n", " | \n", " | \n", " | graph.delete_traces([1])\n", " | ```\n", " | \n", " | extend_traces(self, update, indices=(0,), max_points=None)\n", " | Append data points to existing traces in the Plotly graph.\n", " | \n", " | Args:\n", " | update (dict):\n", " | dict where keys are the graph attribute strings\n", " | and values are arrays of arrays with values to extend.\n", " | \n", " | Each array in the array will extend a trace.\n", " | \n", " | Valid keys include:\n", " | 'x', 'y', 'text,\n", " | 'marker.color', 'marker.size', 'marker.symbol',\n", " | 'marker.line.color', 'marker.line.width'\n", " | \n", " | indices (list, int):\n", " | Specify which traces to apply the `update` dict to.\n", " | If indices are not given, the update will apply to\n", " | the traces in order.\n", " | \n", " | max_points (int or dict, optional):\n", " | If specified, then only show the `max_points` most\n", " | recent points in the graph.\n", " | This is useful to prevent traces from becoming too\n", " | large (and slow) or for creating \"windowed\" graphs\n", " | in monitoring applications.\n", " | \n", " | To set max_points to different values for each trace\n", " | or attribute, set max_points to a dict mapping keys\n", " | to max_points values. See the examples below.\n", " | \n", " | Examples:\n", " | Initialization - Start each example below with this setup:\n", " | ```\n", " | from plotly.widgets import GraphWidget\n", " | from IPython.display import display\n", " | \n", " | graph = GraphWidget()\n", " | graph.plot([\n", " | {'x': [], 'y': []},\n", " | {'x': [], 'y': []}\n", " | ])\n", " | \n", " | display(graph)\n", " | ```\n", " | \n", " | Example 1 - Extend the first trace with x and y data\n", " | ```\n", " | graph.extend_traces({'x': [[1, 2, 3]], 'y': [[10, 20, 30]]},\n", " | indices=[0])\n", " | ```\n", " | \n", " | Example 2 - Extend the second trace with x and y data\n", " | ```\n", " | graph.extend_traces({'x': [[1, 2, 3]], 'y': [[10, 20, 30]]},\n", " | indices=[1])\n", " | ```\n", " | \n", " | Example 3 - Extend the first two traces with x and y data\n", " | ```\n", " | graph.extend_traces({\n", " | 'x': [[1, 2, 3], [2, 3, 4]],\n", " | 'y': [[10, 20, 30], [3, 4, 3]]\n", " | }, indices=[0, 1])\n", " | ```\n", " | \n", " | Example 4 - Extend the first trace with x and y data and\n", " | limit the length of data in that trace to 50\n", " | points.\n", " | ```\n", " | \n", " | graph.extend_traces({\n", " | 'x': [range(100)],\n", " | 'y': [range(100)]\n", " | }, indices=[0, 1], max_points=50)\n", " | ```\n", " | \n", " | Example 5 - Extend the first and second trace with x and y data\n", " | and limit the length of data in the first trace to\n", " | 25 points and the second trace to 50 points.\n", " | ```\n", " | new_points = range(100)\n", " | graph.extend_traces({\n", " | 'x': [new_points, new_points],\n", " | 'y': [new_points, new_points]\n", " | },\n", " | indices=[0, 1],\n", " | max_points={\n", " | 'x': [25, 50],\n", " | 'y': [25, 50]\n", " | }\n", " | )\n", " | ```\n", " | \n", " | Example 6 - Update other attributes, like marker colors and\n", " | sizes and text\n", " | ```\n", " | # Initialize a plot with some empty attributes\n", " | graph.plot([{\n", " | 'x': [],\n", " | 'y': [],\n", " | 'text': [],\n", " | 'marker': {\n", " | 'size': [],\n", " | 'color': []\n", " | }\n", " | }])\n", " | # Append some data into those attributes\n", " | graph.extend_traces({\n", " | 'x': [[1, 2, 3]],\n", " | 'y': [[10, 20, 30]],\n", " | 'text': [['A', 'B', 'C']],\n", " | 'marker.size': [[10, 15, 20]],\n", " | 'marker.color': [['blue', 'red', 'orange']]\n", " | }, indices=[0])\n", " | ```\n", " | \n", " | Example 7 - Live-update a graph over a few seconds\n", " | ```\n", " | import time\n", " | \n", " | graph.plot([{'x': [], 'y': []}])\n", " | for i in range(10):\n", " | graph.extend_traces({\n", " | 'x': [[i]],\n", " | 'y': [[i]]\n", " | }, indices=[0])\n", " | \n", " | time.sleep(0.5)\n", " | ```\n", " | \n", " | hover(self, *hover_objs)\n", " | Show hover labels over the points specified in hover_obj.\n", " | \n", " | Hover labels are the labels that normally appear when the\n", " | mouse hovers over points in the plotly graph.\n", " | \n", " | Args:\n", " | hover_objs (tuple of dicts):\n", " | Specifies which points to place hover labels over.\n", " | \n", " | The location of the hover labels is described by a dict with\n", " | keys and'xval' and/or 'yval' or 'curveNumber' and 'pointNumber'\n", " | and optional keys 'hovermode' and 'subplot'\n", " | \n", " | 'xval' and 'yval' specify the (x, y) coordinates to\n", " | place the label.\n", " | 'xval' and 'yval need to be close to a point drawn in a graph.\n", " | \n", " | 'curveNumber' and 'pointNumber' specify the trace number and\n", " | the index theof the point in that trace respectively.\n", " | \n", " | 'subplot' describes which axes to the coordinates refer to.\n", " | By default, it is equal to 'xy'. For example, to specify the\n", " | second x-axis and the third y-axis, set 'subplot' to 'x2y3'\n", " | \n", " | 'hovermode' is either 'closest', 'x', or 'y'.\n", " | When set to 'x', all data sharing the same 'x' coordinate will\n", " | be shown on screen with corresponding trace labels.\n", " | When set to 'y' all data sharing the same 'y' coordinates will\n", " | be shown on the screen with corresponding trace labels.\n", " | When set to 'closest', information about the data point closest\n", " | to where the viewer is hovering will appear.\n", " | \n", " | Note: If 'hovermode' is 'x', only 'xval' needs to be set.\n", " | If 'hovermode' is 'y', only 'yval' needs to be set.\n", " | If 'hovermode' is 'closest', 'xval' and 'yval' both\n", " | need to be set.\n", " | \n", " | Note: 'hovermode' can be toggled by the user in the graph\n", " | toolbar.\n", " | \n", " | Note: It is not currently possible to apply multiple hover\n", " | labels to points on different axes.\n", " | \n", " | Note: `hover` can only be called with multiple dicts if\n", " | 'curveNumber' and 'pointNumber' are the keys of the dicts\n", " | \n", " | Examples:\n", " | Initialization - Start each example below with this setup:\n", " | ```\n", " | from plotly.widgets import GraphWidget\n", " | from IPython.display import display\n", " | \n", " | graph = GraphWidget('https://plotly.com/~chris/3979')\n", " | display(graph)\n", " | ```\n", " | \n", " | Example 1 - Apply a label to the (x, y) point (3, 2)\n", " | ```\n", " | graph.hover({'xval': 3, 'yval': 2, 'hovermode': 'closest'})\n", " | ```\n", " | \n", " | Example 2 -Apply a labels to all the points with the x coordinate 3\n", " | ```\n", " | graph.hover({'xval': 3, 'hovermode': 'x'})\n", " | ```\n", " | \n", " | Example 3 - Apply a label to the first point of the first trace\n", " | and the second point of the second trace.\n", " | ```\n", " | graph.hover({'curveNumber': 0, 'pointNumber': 0},\n", " | {'curveNumber': 1, 'pointNumber': 1})\n", " | ```\n", " | \n", " | on_click(self, callback, remove=False)\n", " | Assign a callback to click events propagated\n", " | by clicking on point(s) in the Plotly graph.\n", " | \n", " | Args:\n", " | callback (function): Callback function this is called\n", " | on click events with the signature:\n", " | callback(widget, hover_obj) -> None\n", " | \n", " | Args:\n", " | widget (GraphWidget): The current instance\n", " | of the graph widget that this callback is assigned to.\n", " | \n", " | click_obj (dict): a nested dict that describes\n", " | which point(s) were clicked on.\n", " | \n", " | click_obj example:\n", " | [\n", " | {\n", " | 'curveNumber': 1,\n", " | 'pointNumber': 2,\n", " | 'x': 4,\n", " | 'y': 14\n", " | }\n", " | ]\n", " | \n", " | remove (bool, optional): If False, attach the callback.\n", " | If True, remove the callback. Defaults to False.\n", " | \n", " | \n", " | Returns:\n", " | None\n", " | \n", " | Example:\n", " | ```\n", " | from IPython.display import display\n", " | def message_handler(widget, msg):\n", " | display(widget._graph_url)\n", " | display(msg)\n", " | \n", " | g = GraphWidget('https://plotly.com/~chris/3375')\n", " | display(g)\n", " | \n", " | g.on_click(message_handler)\n", " | ```\n", " | \n", " | on_hover(self, callback, remove=False)\n", " | Assign a callback to hover events propagated\n", " | by hovering over points in the Plotly graph.\n", " | \n", " | Args:\n", " | callback (function): Callback function this is called\n", " | on hover events with the signature:\n", " | callback(widget, hover_obj) -> None\n", " | \n", " | Args:\n", " | widget (GraphWidget): The current instance\n", " | of the graph widget that this callback is assigned to.\n", " | \n", " | hover_obj (dict): a nested dict that describes\n", " | which point(s) was hovered over.\n", " | \n", " | hover_obj example:\n", " | [\n", " | {\n", " | 'curveNumber': 1,\n", " | 'pointNumber': 2,\n", " | 'x': 4,\n", " | 'y': 14\n", " | }\n", " | ]\n", " | \n", " | remove (bool, optional): If False, attach the callback.\n", " | If True, remove the callback. Defaults to False.\n", " | \n", " | \n", " | Returns:\n", " | None\n", " | \n", " | Example:\n", " | ```\n", " | from IPython.display import display\n", " | def message_handler(widget, hover_msg):\n", " | display(widget._graph_url)\n", " | display(hover_msg)\n", " | \n", " | g = GraphWidget('https://plotly.com/~chris/3375')\n", " | display(g)\n", " | \n", " | g.on_hover(message_handler)\n", " | ```\n", " | \n", " | on_zoom(self, callback, remove=False)\n", " | Assign a callback to zoom events propagated\n", " | by zooming in regions in the Plotly graph.\n", " | \n", " | Args:\n", " | callback (function): Callback function this is called\n", " | on zoom events with the signature:\n", " | callback(widget, ranges) -> None\n", " | \n", " | Args:\n", " | widget (GraphWidget): The current instance\n", " | of the graph widget that this callback is assigned to.\n", " | \n", " | ranges (dict): A description of the\n", " | region that was zoomed into.\n", " | \n", " | ranges example:\n", " | {\n", " | 'x': [1.8399058038561549, 2.16443359662],\n", " | 'y': [4.640902872777017, 7.855677154582]\n", " | }\n", " | \n", " | remove (bool, optional): If False, attach the callback.\n", " | If True, remove the callback. Defaults to False.\n", " | \n", " | Returns:\n", " | None\n", " | \n", " | Example:\n", " | ```\n", " | from IPython.display import display\n", " | def message_handler(widget, ranges):\n", " | display(widget._graph_url)\n", " | display(ranges)\n", " | \n", " | g = GraphWidget('https://plotly.com/~chris/3375')\n", " | display(g)\n", " | \n", " | g.on_zoom(message_handler)\n", " | ```\n", " | \n", " | plot(self, figure_or_data, validate=True)\n", " | Plot figure_or_data in the Plotly graph widget.\n", " | \n", " | Args:\n", " | figure_or_data (dict, list, or plotly.graph_obj object):\n", " | The standard Plotly graph object that describes Plotly\n", " | graphs as used in `plotly.plotly.plot`. See examples\n", " | of the figure_or_data in https://plotly.com/python/\n", " | \n", " | Returns: None\n", " | \n", " | Example 1 - Graph a scatter plot:\n", " | ```\n", " | from plotly.graph_objs import Scatter\n", " | g = GraphWidget()\n", " | g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])])\n", " | ```\n", " | \n", " | Example 2 - Graph a scatter plot with a title:\n", " | ```\n", " | from plotly.graph_objs import Scatter, Figure, Data\n", " | fig = Figure(\n", " | data = Data([\n", " | Scatter(x=[1, 2, 3], y=[20, 15, 13])\n", " | ]),\n", " | layout = Layout(title='Experimental Data')\n", " | )\n", " | \n", " | g = GraphWidget()\n", " | g.plot(fig)\n", " | ```\n", " | \n", " | Example 3 - Clear a graph widget\n", " | ```\n", " | from plotly.graph_objs import Scatter, Figure\n", " | g = GraphWidget()\n", " | g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])])\n", " | \n", " | # Now clear it\n", " | g.plot({}) # alternatively, g.plot(Figure())\n", " | ```\n", " | \n", " | relayout(self, layout)\n", " | Update the layout of the Plotly graph.\n", " | \n", " | Args:\n", " | layout (dict):\n", " | dict where keys are the graph attribute strings\n", " | and values are the value of the graph attribute.\n", " | \n", " | To update graph objects that are nested, like\n", " | the title of an axis, combine the keys with a period\n", " | e.g. `xaxis.title`. To set a value of an element in an array,\n", " | like an axis's range, use brackets, e.g. 'xaxis.range[0]'.\n", " | To replace an entire nested object, just specify the value to\n", " | the sub-object. See example 4 below.\n", " | \n", " | See all of the layout attributes in our reference documentation\n", " | https://plotly.com/python/reference/#Layout\n", " | Or by calling `help` on `plotly.graph_objs.Layout`\n", " | \n", " | Examples - Start each example below with this setup:\n", " | Initialization:\n", " | ```\n", " | from plotly.widgets import GraphWidget\n", " | from IPython.display import display\n", " | \n", " | graph = GraphWidget('https://plotly.com/~chris/3979')\n", " | display(graph)\n", " | ```\n", " | \n", " | Example 1 - Update the title\n", " | ```\n", " | graph.relayout({'title': 'Experimental results'})\n", " | ```\n", " | \n", " | Example 2 - Update the xaxis range\n", " | ```\n", " | graph.relayout({'xaxis.range': [-1, 6]})\n", " | ```\n", " | \n", " | Example 3 - Update the first element of the xaxis range\n", " | ```\n", " | graph.relayout({'xaxis.range[0]': -3})\n", " | ```\n", " | \n", " | Example 4 - Replace the entire xaxis object\n", " | ```\n", " | graph.relayout({'xaxis': {'title': 'Experimental results'}})\n", " | ```\n", " | \n", " | reorder_traces(self, current_indices, new_indices=None)\n", " | Reorder the traces in a graph.\n", " | \n", " | The order of the traces determines the order of the legend entries\n", " | and the layering of the objects drawn in the graph, i.e. the first\n", " | trace is drawn first and the second trace is drawn on top of the\n", " | first trace.\n", " | \n", " | Args:\n", " | current_indices (list[int]): The index of the traces to reorder.\n", " | \n", " | new_indices (list[int], optional): The index of the traces\n", " | specified by `current_indices` after ordering.\n", " | If None, then move the traces to the end.\n", " | \n", " | Examples:\n", " | Example 1 - Move the first trace to the second to last\n", " | position, the second trace to the last position\n", " | ```\n", " | graph.move_traces([0, 1])\n", " | ```\n", " | \n", " | Example 2 - Move the first trace to the second position,\n", " | the second trace to the first position.\n", " | ```\n", " | graph.move_traces([0], [1])\n", " | ```\n", " | \n", " | restyle(self, update, indices=None)\n", " | Update the style of existing traces in the Plotly graph.\n", " | \n", " | Args:\n", " | update (dict):\n", " | dict where keys are the graph attribute strings\n", " | and values are the value of the graph attribute.\n", " | \n", " | To update graph objects that are nested, like\n", " | a marker's color, combine the keys with a period,\n", " | e.g. `marker.color`. To replace an entire nested object,\n", " | like `marker`, set the value to the object.\n", " | See Example 2 below.\n", " | \n", " | To update an attribute of multiple traces, set the\n", " | value to an list of values. If the list is shorter\n", " | than the number of traces, the values will wrap around.\n", " | Note: this means that for values that are naturally an array,\n", " | like `x` or `colorscale`, you need to wrap the value\n", " | in an extra array,\n", " | i.e. {'colorscale': [[[0, 'red'], [1, 'green']]]}\n", " | \n", " | You can also supply values to different traces with the\n", " | indices argument.\n", " | \n", " | See all of the graph attributes in our reference documentation\n", " | here: https://plotly.com/python/reference or by calling `help` on\n", " | graph objects in `plotly.graph_objs`.\n", " | \n", " | indices (list, optional):\n", " | Specify which traces to apply the update dict to.\n", " | Negative indices are supported.\n", " | If indices are not given, the update will apply to\n", " | *all* traces.\n", " | \n", " | Examples:\n", " | Initialization - Start each example below with this setup:\n", " | ```\n", " | from plotly.widgets import GraphWidget\n", " | from IPython.display import display\n", " | \n", " | graph = GraphWidget()\n", " | display(graph)\n", " | ```\n", " | \n", " | Example 1 - Set `marker.color` to red in every trace in the graph\n", " | ```\n", " | graph.restyle({'marker.color': 'red'})\n", " | ```\n", " | \n", " | Example 2 - Replace `marker` with {'color': 'red'}\n", " | ```\n", " | graph.restyle({'marker': {'color': red'}})\n", " | ```\n", " | \n", " | Example 3 - Set `marker.color` to red\n", " | in the first trace of the graph\n", " | ```\n", " | graph.restyle({'marker.color': 'red'}, indices=[0])\n", " | ```\n", " | \n", " | Example 4 - Set `marker.color` of all of the traces to\n", " | alternating sequences of red and green\n", " | ```\n", " | graph.restyle({'marker.color': ['red', 'green']})\n", " | ```\n", " | \n", " | Example 5 - Set just `marker.color` of the first two traces\n", " | to red and green\n", " | ```\n", " | graph.restyle({'marker.color': ['red', 'green']}, indices=[0, 1])\n", " | ```\n", " | \n", " | Example 6 - Set multiple attributes of all of the traces\n", " | ```\n", " | graph.restyle({\n", " | 'marker.color': 'red',\n", " | 'line.color': 'green'\n", " | })\n", " | ```\n", " | \n", " | Example 7 - Update the data of the first trace\n", " | ```\n", " | graph.restyle({\n", " | 'x': [[1, 2, 3]],\n", " | 'y': [[10, 20, 30]],\n", " | }, indices=[0])\n", " | ```\n", " | \n", " | Example 8 - Update the data of the first two traces\n", " | ```\n", " | graph.restyle({\n", " | 'x': [[1, 2, 3],\n", " | [1, 2, 4]],\n", " | 'y': [[10, 20, 30],\n", " | [5, 8, 14]],\n", " | }, indices=[0, 1])\n", " | ```\n", " | \n", " | save(self, ignore_defaults=False, filename='')\n", " | Save a copy of the current state of the widget in plotly.\n", " | \n", " | :param (bool) ignore_defaults: Auto-fill in unspecified figure keys?\n", " | :param (str) filename: Name of the file on plotly.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | url\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors inherited from ipywidgets.widgets.widget.DOMWidget:\n", " | \n", " | background_color\n", " | A string holding a valid HTML color such as 'blue', '#060482', '#A80'\n", " | \n", " | border_color\n", " | A string holding a valid HTML color such as 'blue', '#060482', '#A80'\n", " | \n", " | border_radius\n", " | A casting version of the unicode trait.\n", " | \n", " | border_style\n", " | An enum of strings where the case should be ignored.\n", " | \n", " | border_width\n", " | A casting version of the unicode trait.\n", " | \n", " | color\n", " | A string holding a valid HTML color such as 'blue', '#060482', '#A80'\n", " | \n", " | font_family\n", " | A trait for unicode strings.\n", " | \n", " | font_size\n", " | A casting version of the unicode trait.\n", " | \n", " | font_style\n", " | An enum of strings where the case should be ignored.\n", " | \n", " | font_weight\n", " | An enum of strings where the case should be ignored.\n", " | \n", " | height\n", " | A casting version of the unicode trait.\n", " | \n", " | margin\n", " | A casting version of the unicode trait.\n", " | \n", " | padding\n", " | A casting version of the unicode trait.\n", " | \n", " | visible\n", " | A boolean (True, False) trait.\n", " | \n", " | width\n", " | A casting version of the unicode trait.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Methods inherited from ipywidgets.widgets.widget.Widget:\n", " | \n", " | __del__(self)\n", " | Object disposal\n", " | \n", " | add_traits(self, **traits)\n", " | Dynamically add trait attributes to the Widget.\n", " | \n", " | close(self)\n", " | Close method.\n", " | \n", " | Closes the underlying comm.\n", " | When the comm is closed, all of the widget views are automatically\n", " | removed from the front-end.\n", " | \n", " | get_state(self, key=None)\n", " | Gets the widget state, or a piece of it.\n", " | \n", " | Parameters\n", " | ----------\n", " | key : unicode or iterable (optional)\n", " | A single property's name or iterable of property names to get.\n", " | \n", " | Returns\n", " | -------\n", " | state : dict of states\n", " | metadata : dict\n", " | metadata for each field: {key: metadata}\n", " | \n", " | hold_sync(*args, **kwds)\n", " | Hold syncing any state until the outermost context manager exits\n", " | \n", " | on_displayed(self, callback, remove=False)\n", " | (Un)Register a widget displayed callback.\n", " | \n", " | Parameters\n", " | ----------\n", " | callback: method handler\n", " | Must have a signature of::\n", " | \n", " | callback(widget, **kwargs)\n", " | \n", " | kwargs from display are passed through without modification.\n", " | remove: bool\n", " | True if the callback should be unregistered.\n", " | \n", " | on_msg(self, callback, remove=False)\n", " | (Un)Register a custom msg receive callback.\n", " | \n", " | Parameters\n", " | ----------\n", " | callback: callable\n", " | callback will be passed three arguments when a message arrives::\n", " | \n", " | callback(widget, content, buffers)\n", " | \n", " | remove: bool\n", " | True if the callback should be unregistered.\n", " | \n", " | open(self)\n", " | Open a comm to the frontend if one isn't already open.\n", " | \n", " | send(self, content, buffers=None)\n", " | Sends a custom msg to the widget model in the front-end.\n", " | \n", " | Parameters\n", " | ----------\n", " | content : dict\n", " | Content of the message to send.\n", " | buffers : list of binary buffers\n", " | Binary buffers to send with message\n", " | \n", " | send_state(self, key=None)\n", " | Sends the widget state, or a piece of it, to the front-end.\n", " | \n", " | Parameters\n", " | ----------\n", " | key : unicode, or iterable (optional)\n", " | A single property's name or iterable of property names to sync with the front-end.\n", " | \n", " | set_state(self, sync_data)\n", " | Called when a state is received from the front-end.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Static methods inherited from ipywidgets.widgets.widget.Widget:\n", " | \n", " | handle_comm_opened(comm, msg)\n", " | Static method, called when a widget is constructed.\n", " | \n", " | on_widget_constructed(callback)\n", " | Registers a callback to be called when a widget is constructed.\n", " | \n", " | The callback must have the following signature:\n", " | callback(widget)\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors inherited from ipywidgets.widgets.widget.Widget:\n", " | \n", " | comm\n", " | A trait whose value must be an instance of a specified class.\n", " | \n", " | The value can also be an instance of a subclass of the specified class.\n", " | \n", " | Subclasses can declare default classes by overriding the klass attribute\n", " | \n", " | keys\n", " | An instance of a Python list.\n", " | \n", " | model_id\n", " | Gets the model id of this widget.\n", " | \n", " | If a Comm doesn't exist yet, a Comm will be created automagically.\n", " | \n", " | msg_throttle\n", " | An int trait.\n", " | \n", " | version\n", " | An int trait.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data and other attributes inherited from ipywidgets.widgets.widget.Widget:\n", " | \n", " | widget_types = {'IPython.Accordion': " ], "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 c:\\users\\brand\\appdata\\local\\temp\\pip-req-build-vu6mjzdi\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", " Running setup.py install for publisher: started\n", " Running setup.py install for publisher: finished with status 'done'\n", "Successfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Python27\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", " \"You should import from nbconvert instead.\", ShimWarning)\n", "C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", " warnings.warn('Did you \"Save\" this notebook before running this command? '\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", " \n", "import publisher\n", "publisher.publish(\n", " 'widget_app.ipynb', 'python/widget-app/', 'IPython Widgets',\n", " 'Interactive Data Analysis with Plotly',\n", " title = 'Interactive Data Analysis with ipywidgets',\n", " name = 'Interactive Data Analysis with ipywidgets',\n", " has_thumbnail='true', thumbnail='thumbnail/multi-widget.jpg', \n", " language='python', page_type='example_index', \n", " display_as='chart_events', order=23,\n", " ipynb= '~notebook_demo/90')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.14" } }, "nbformat": 4, "nbformat_minor": 1 }