{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To install Plotly's python package, use the package manager **pip** inside your terminal.
\n", "If you don't have **pip** installed on your machine, [click here](https://pip.pypa.io/en/latest/installing.html) for pip's installation instructions.\n", "
\n", "
\n", "`$ pip install plotly`\n", "
or\n", "
`$ sudo pip install plotly`\n", "
\n", "
\n", "Plotly's Python package is [updated frequently](https://github.com/plotly/plotly.py/blob/master/CHANGELOG.md)! To upgrade, run:\n", "
\n", "
\n", "`$ pip install plotly --upgrade`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialization for Online Plotting\n", "Plotly provides a web-service for hosting graphs! Create a [free account](https://plotly.com/api_signup) to get started. Graphs are saved inside your online Plotly account and you control the privacy. Public hosting is free, for private hosting, check out our [paid plans](https://plotly.com/products/cloud/).\n", "
\n", "
\n", "After installing the Plotly package, you're ready to fire up python:\n", "
\n", "
\n", "`$ python`\n", "
\n", "
\n", "and set your credentials:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import plotly \n", "plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You'll need to replace **'DemoAccount'** and **'lr1c37zw81'** with *your* Plotly username and [API key](https://plotly.com/settings/api).
\n", "Find your API key [here](https://plotly.com/settings/api).\n", "
\n", "
\n", "The initialization step places a special **.plotly/.credentials** file in your home directory. Your **~/.plotly/.credentials** file should look something like this:\n", "
\n", "```\n", "{\n", " \"username\": \"DemoAccount\",\n", " \"stream_ids\": [\"ylosqsyet5\", \"h2ct8btk1s\", \"oxz4fm883b\"],\n", " \"api_key\": \"lr1c37zw81\"\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Online Plot Privacy\n", "\n", "Plot can be set to three different type of privacies: public, private or secret.\n", "- **public**: Anyone can view this graph. It will appear in your profile and can appear in search engines. You do not need to be logged in to Plotly to view this chart.\n", "- **private**: Only you can view this plot. It will not appear in the Plotly feed, your profile, or search engines. You must be logged in to Plotly to view this graph. You can privately share this graph with other Plotly users in your online Plotly account and they will need to be logged in to view this plot.\n", "- **secret**: Anyone with this secret link can view this chart. It will not appear in the Plotly feed, your profile, or search engines. If it is embedded inside a webpage or an IPython notebook, anybody who is viewing that page will be able to view the graph. You do not need to be logged in to view this plot.\n", "\n", "By default all plots are set to **public**. Users with free account have the permission to keep one private plot. If you need to save private plots, [upgrade to a pro account](https://plotly.com/plans). If you're a [Personal or Professional user](https://plotly.com/settings/subscription/?modal=true&utm_source=api-docs&utm_medium=support-oss) and would like the default setting for your plots to be private, you can edit your Plotly configuration:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import plotly \n", "plotly.tools.set_config_file(world_readable=False,\n", " sharing='private')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more examples on privacy settings please visit [Python privacy documentation](https://plotly.com/python/privacy/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Special Instructions for [Chart Studio Enterprise](https://plotly.com/product/enterprise/) Users " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Your API key for account on the public cloud will be different than the API key in Chart Studio Enterprise. Visit https://plotly.your-company.com/settings/api/ to find your Chart Studio Enterprise API key. Remember to replace \"your-company.com\" with the URL of your Chart Studio Enterprise server.\n", "If your company has a Chart Studio Enterprise server, change the Python API endpoint so that it points to your company's Plotly server instead of Plotly's cloud.\n", "
\n", "
\n", "In python, enter:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import plotly \n", "plotly.tools.set_config_file(plotly_domain='https://plotly.your-company.com',\n", " plotly_streaming_domain='https://stream-plotly.your-company.com')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make sure to replace **\"your-company.com\"** with the URL of *your* Chart Studio Enterprise server." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Additionally, you can set your configuration so that you generate **private plots by default**. For more information on privacy settings see: https://plotly.com/python/privacy/
\n", "
\n", "In python, enter:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import plotly \n", "plotly.tools.set_config_file(plotly_domain='https://plotly.your-company.com',\n", " plotly_streaming_domain='https://stream-plotly.your-company.com', \n", " world_readable=False,\n", " sharing='private')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotly Using virtualenv\n", "Python's `virtualenv` allows us create multiple working Python environments which can each use different versions of packages. We can use `virtualenv` from the command line to create an environment using plotly.py version 3.3.0 and a separate one using plotly.py version 2.7.0. See [the virtualenv documentation](https://virtualenv.pypa.io/en/stable) for more info.\n", "\n", "**Install virtualenv globally**\n", "
`$ sudo pip install virtualenv`\n", " \n", "**Create your virtualenvs**\n", "
`$ mkdir ~/.virtualenvs`\n", "
`$ cd ~/.virtualenvs`\n", "
`$ python -m venv plotly2.7`\n", "
`$ python -m venv plotly3.3`\n", "\n", "**Activate the virtualenv.**\n", "You will see the name of your virtualenv in parenthesis next to the input promt.\n", "
`$ source ~/.virtualenvs/plotly2.7/bin/activate`\n", "
`(plotly2.7) $`\n", "\n", "**Install plotly locally to virtualenv** (note that we don't use sudo).\n", "
`(plotly2.7) $ pip install plotly==2.7`\n", "\n", "**Deactivate to exit**\n", "
\n", "`(plotly2.7) $ deactivate`\n", "
`$`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Jupyter Setup\n", "**Install Jupyter into a virtualenv**\n", "
`$ source ~/.virtualenvs/plotly3.3/bin/activate`\n", "
`(plotly3.3) $ pip install notebook`\n", "\n", "**Start the Jupyter kernel from a virtualenv**\n", "
`(plotly3.3) $ jupyter notebook`\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Start Plotting Online\n", "When plotting online, the plot and data will be saved to your cloud account. There are two methods for plotting online: `py.plot()` and `py.iplot()`. Both options create a unique url for the plot and save it in your Plotly account.\n", "- Use `py.plot()` to return the unique url and optionally open the url.\n", "- Use `py.iplot()` when working in a Jupyter Notebook to display the plot in the notebook.\n", "\n", "Copy and paste one of the following examples to create your first hosted Plotly graph using the Plotly Python library:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://plotly.com/~tobin/22'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "trace0 = go.Scatter(\n", " x=[1, 2, 3, 4],\n", " y=[10, 15, 13, 17]\n", ")\n", "trace1 = go.Scatter(\n", " x=[1, 2, 3, 4],\n", " y=[16, 5, 11, 9]\n", ")\n", "data = [trace0, trace1]\n", "\n", "py.plot(data, filename = 'basic-line', auto_open=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Checkout the docstrings for more information:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function plot in module plotly.plotly.plotly:\n", "\n", "plot(figure_or_data, validate=True, **plot_options)\n", " Create a unique url for this plot in Plotly and optionally open url.\n", " \n", " plot_options keyword arguments:\n", " filename (string) -- the name that will be associated with this figure\n", " fileopt ('new' | 'overwrite' | 'extend' | 'append') -- 'new' creates a\n", " 'new': create a new, unique url for this plot\n", " 'overwrite': overwrite the file associated with `filename` with this\n", " 'extend': add additional numbers (data) to existing traces\n", " 'append': add additional traces to existing data lists\n", " auto_open (default=True) -- Toggle browser options\n", " True: open this plot in a new browser tab\n", " False: do not open plot in the browser, but do return the unique url\n", " sharing ('public' | 'private' | 'secret') -- Toggle who can view this\n", " graph\n", " - 'public': Anyone can view this graph. It will appear in your profile\n", " and can appear in search engines. You do not need to be\n", " logged in to Plotly to view this chart.\n", " - 'private': Only you can view this plot. It will not appear in the\n", " Plotly feed, your profile, or search engines. You must be\n", " logged in to Plotly to view this graph. You can privately\n", " share this graph with other Plotly users in your online\n", " Plotly account and they will need to be logged in to\n", " view this plot.\n", " - 'secret': Anyone with this secret link can view this chart. It will\n", " not appear in the Plotly feed, your profile, or search\n", " engines. If it is embedded inside a webpage or an IPython\n", " notebook, anybody who is viewing that page will be able to\n", " view the graph. You do not need to be logged in to view\n", " this plot.\n", " world_readable (default=True) -- Deprecated: use \"sharing\".\n", " Make this figure private/public\n", "\n" ] } ], "source": [ "import plotly.plotly as py\n", "help(py.plot)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "trace0 = go.Scatter(\n", " x=[1, 2, 3, 4],\n", " y=[10, 15, 13, 17]\n", ")\n", "trace1 = go.Scatter(\n", " x=[1, 2, 3, 4],\n", " y=[16, 5, 11, 9]\n", ")\n", "data = [trace0, trace1]\n", "\n", "py.iplot(data, filename = 'basic-line')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See more examples in our [IPython notebook documentation](https://plotly.com/ipython-notebooks/) or check out the `py.iplot()` docstring for more information." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function iplot in module chart_studio.plotly.plotly:\n", "\n", "iplot(figure_or_data, **plot_options)\n", " Create a unique url for this plot in Plotly and open in IPython.\n", " \n", " plot_options keyword arguments:\n", " filename (string) -- the name that will be associated with this figure\n", " fileopt ('new' | 'overwrite' | 'extend' | 'append')\n", " - 'new': create a new, unique url for this plot\n", " - 'overwrite': overwrite the file associated with `filename` with this\n", " - 'extend': add additional numbers (data) to existing traces\n", " - 'append': add additional traces to existing data lists\n", " sharing ('public' | 'private' | 'secret') -- Toggle who can view this graph\n", " - 'public': Anyone can view this graph. It will appear in your profile\n", " and can appear in search engines. You do not need to be\n", " logged in to Plotly to view this chart.\n", " - 'private': Only you can view this plot. It will not appear in the\n", " Plotly feed, your profile, or search engines. You must be\n", " logged in to Plotly to view this graph. You can privately\n", " share this graph with other Plotly users in your online\n", " Plotly account and they will need to be logged in to\n", " view this plot.\n", " - 'secret': Anyone with this secret link can view this chart. It will\n", " not appear in the Plotly feed, your profile, or search\n", " engines. If it is embedded inside a webpage or an IPython\n", " notebook, anybody who is viewing that page will be able to\n", " view the graph. You do not need to be logged in to view\n", " this plot.\n", " world_readable (default=True) -- Deprecated: use \"sharing\".\n", " Make this figure private/public\n", "\n" ] } ], "source": [ "import plotly.plotly as py\n", "help(py.iplot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also create plotly graphs with **matplotlib** syntax. Learn more in our [matplotlib documentation](https://plotly.com/matplotlib/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialization for Offline Plotting\n", "Plotly Offline allows you to create graphs offline and save them locally. There are also two methods for plotting offline: `plotly.offline.plot()` and `plotly.offline.iplot()`. \n", "- Use `plotly.offline.plot()` to create and standalone HTML that is saved locally and opened inside your web browser.\n", "- Use `plotly.offline.iplot()` when working offline in a Jupyter Notebook to display the plot in the notebook.\n", "\n", "Check your Plotly version, version 1.9.4+ is needed for offline plotting:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.9.0'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy and paste one of the following examples to create your first offline Plotly graph using the Plotly Python library:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'temp-plot.html'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "import plotly.graph_objs as go\n", "\n", "plotly.offline.plot({\n", " \"data\": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],\n", " \"layout\": go.Layout(title=\"hello world\")\n", "}, auto_open=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Learn more by calling `help()`:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function plot in module plotly.offline.offline:\n", "\n", "plot(figure_or_data, show_link=True, link_text='Export to plot.ly', validate=True, output_type='file', include_plotlyjs=True, filename='temp-plot.html', auto_open=True, image=None, image_filename='plot_image', image_width=800, image_height=600, config=None, include_mathjax=False)\n", " Create a plotly graph locally as an HTML document or string.\n", " \n", " Example:\n", " ```\n", " from plotly.offline import plot\n", " import plotly.graph_objs as go\n", " \n", " plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html')\n", " # We can also download an image of the plot by setting the image parameter\n", " # to the image format we want\n", " plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html',\n", " image='jpeg')\n", " ```\n", " More examples below.\n", " \n", " figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or\n", " dict or list that describes a Plotly graph.\n", " See https://plotly.com/python/ for examples of\n", " graph descriptions.\n", " \n", " Keyword arguments:\n", " show_link (default=True) -- display a link in the bottom-right corner of\n", " of the chart that will export the chart to Chart Studio Cloud or\n", " Chart Studio Enterprise\n", " link_text (default='Export to plot.ly') -- the text of export link\n", " validate (default=True) -- validate that all of the keys in the figure\n", " are valid? omit if your version of plotly.js has become outdated\n", " with your version of graph_reference.json or if you need to include\n", " extra, unnecessary keys in your figure.\n", " output_type ('file' | 'div' - default 'file') -- if 'file', then\n", " the graph is saved as a standalone HTML file and `plot`\n", " returns None.\n", " If 'div', then `plot` returns a string that just contains the\n", " HTML
that contains the graph and the script to generate the\n", " graph.\n", " Use 'file' if you want to save and view a single graph at a time\n", " in a standalone HTML file.\n", " Use 'div' if you are embedding these graphs in an HTML file with\n", " other graphs or HTML markup, like a HTML report or an website.\n", " include_plotlyjs (True | False | 'cdn' | 'directory' | path - default=True)\n", " Specifies how the plotly.js library is included in the output html\n", " file or div string.\n", " \n", " If True, a script tag containing the plotly.js source code (~3MB)\n", " is included in the output. HTML files generated with this option are\n", " fully self-contained and can be used offline.\n", " \n", " If 'cdn', a script tag that references the plotly.js CDN is included\n", " in the output. HTML files generated with this option are about 3MB\n", " smaller than those generated with include_plotlyjs=True, but they\n", " require an active internet connection in order to load the plotly.js\n", " library.\n", " \n", " If 'directory', a script tag is included that references an external\n", " plotly.min.js bundle that is assumed to reside in the same\n", " directory as the HTML file. If output_type='file' then the\n", " plotly.min.js bundle is copied into the directory of the resulting\n", " HTML file. If a file named plotly.min.js already exists in the output\n", " directory then this file is left unmodified and no copy is performed.\n", " HTML files generated with this option can be used offline, but they\n", " require a copy of the plotly.min.js bundle in the same directory.\n", " This option is useful when many figures will be saved as HTML files in\n", " the same directory because the plotly.js source code will be included\n", " only once per output directory, rather than once per output file.\n", " \n", " If a string that ends in '.js', a script tag is included that\n", " references the specified path. This approach can be used to point\n", " the resulting HTML file to an alternative CDN.\n", " \n", " If False, no script tag referencing plotly.js is included. This is\n", " useful when output_type='div' and the resulting div string will be\n", " placed inside an HTML document that already loads plotly.js. This\n", " option is not advised when output_type='file' as it will result in\n", " a non-functional html file.\n", " filename (default='temp-plot.html') -- The local filename to save the\n", " outputted chart to. If the filename already exists, it will be\n", " overwritten. This argument only applies if `output_type` is 'file'.\n", " auto_open (default=True) -- If True, open the saved file in a\n", " web browser after saving.\n", " This argument only applies if `output_type` is 'file'.\n", " image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets\n", " the format of the image to be downloaded, if we choose to download an\n", " image. This parameter has a default value of None indicating that no\n", " image should be downloaded. Please note: for higher resolution images\n", " and more export options, consider making requests to our image servers.\n", " Type: `help(py.image)` for more details.\n", " image_filename (default='plot_image') -- Sets the name of the file your\n", " image will be saved to. The extension should not be included.\n", " image_height (default=600) -- Specifies the height of the image in `px`.\n", " image_width (default=800) -- Specifies the width of the image in `px`.\n", " config (default=None) -- Plot view options dictionary. Keyword arguments\n", " `show_link` and `link_text` set the associated options in this\n", " dictionary if it doesn't contain them already.\n", " include_mathjax (False | 'cdn' | path - default=False) --\n", " Specifies how the MathJax.js library is included in the output html\n", " file or div string. MathJax is required in order to display labels\n", " with LaTeX typesetting.\n", " \n", " If False, no script tag referencing MathJax.js will be included in the\n", " output. HTML files generated with this option will not be able to\n", " display LaTeX typesetting.\n", " \n", " If 'cdn', a script tag that references a MathJax CDN location will be\n", " included in the output. HTML files generated with this option will be\n", " able to display LaTeX typesetting as long as they have internet access.\n", " \n", " If a string that ends in '.js', a script tag is included that\n", " references the specified path. This approach can be used to point the\n", " resulting HTML file to an alternative CDN.\n", "\n" ] } ], "source": [ "import plotly\n", "help(plotly.offline.plot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When using `plotly.offline.iplot` to plot offline in Jupyter Notebooks, there is an additional initialization step of running: `plotly.offline.init_notebook_mode()` at the start of each notebook session.
See the example below:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plotly.com", "responsive": true, "showLink": false }, "data": [ { "type": "scatter", "uid": "0cd26e74-0eaa-4502-b27a-49c1f93a966d", "x": [ 1, 2, 3, 4 ], "y": [ 4, 3, 2, 1 ] } ], "layout": { "title": { "text": "hello world" } } }, "text/html": [ "
\n", " \n", " \n", "
\n", " \n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly\n", "import plotly.graph_objs as go\n", "\n", "plotly.offline.init_notebook_mode(connected=True)\n", "\n", "plotly.offline.iplot({\n", " \"data\": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],\n", " \"layout\": go.Layout(title=\"hello world\")\n", "})" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function iplot in module plotly.offline.offline:\n", "\n", "iplot(figure_or_data, show_link=False, link_text='Export to plot.ly', validate=True, image=None, filename='plot_image', image_width=800, image_height=600, config=None, auto_play=True, animation_opts=None)\n", " Draw plotly graphs inside an IPython or Jupyter notebook\n", " \n", " figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or\n", " dict or list that describes a Plotly graph.\n", " See https://plotly.com/python/ for examples of\n", " graph descriptions.\n", " \n", " Keyword arguments:\n", " show_link (default=False) -- display a link in the bottom-right corner of\n", " of the chart that will export the chart to\n", " Plotly Cloud or Plotly Enterprise\n", " link_text (default='Export to plot.ly') -- the text of export link\n", " validate (default=True) -- validate that all of the keys in the figure\n", " are valid? omit if your version of plotly.js\n", " has become outdated with your version of\n", " graph_reference.json or if you need to include\n", " extra, unnecessary keys in your figure.\n", " image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets\n", " the format of the image to be downloaded, if we choose to download an\n", " image. This parameter has a default value of None indicating that no\n", " image should be downloaded. Please note: for higher resolution images\n", " and more export options, consider using plotly.io.write_image. See\n", " https://plotly.com/python/static-image-export/ for more details.\n", " filename (default='plot') -- Sets the name of the file your image\n", " will be saved to. The extension should not be included.\n", " image_height (default=600) -- Specifies the height of the image in `px`.\n", " image_width (default=800) -- Specifies the width of the image in `px`.\n", " config (default=None) -- Plot view options dictionary. Keyword arguments\n", " `show_link` and `link_text` set the associated options in this\n", " dictionary if it doesn't contain them already.\n", " auto_play (default=True) -- Whether to automatically start the animation\n", " sequence if the figure contains frames. Has no effect if the figure\n", " does not contain frames.\n", " animation_opts (default=None) -- dict of custom animation parameters to be\n", " passed to the function Plotly.animate in Plotly.js. See\n", " https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js\n", " for available options. Has no effect if the figure\n", " does not contain frames, or auto_play is False.\n", " \n", " Example:\n", " ```\n", " from plotly.offline import init_notebook_mode, iplot\n", " init_notebook_mode()\n", " iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])\n", " # We can also download an image of the plot by setting the image to the\n", " format you want. e.g. `image='png'`\n", " iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')\n", " ```\n", " \n", " animation_opts Example:\n", " ```\n", " from plotly.offline import iplot\n", " figure = {'data': [{'x': [0, 1], 'y': [0, 1]}],\n", " 'layout': {'xaxis': {'range': [0, 5], 'autorange': False},\n", " 'yaxis': {'range': [0, 5], 'autorange': False},\n", " 'title': 'Start Title'},\n", " 'frames': [{'data': [{'x': [1, 2], 'y': [1, 2]}]},\n", " {'data': [{'x': [1, 4], 'y': [1, 4]}]},\n", " {'data': [{'x': [3, 4], 'y': [3, 4]}],\n", " 'layout': {'title': 'End Title'}}]}\n", " iplot(figure,animation_opts=\"{frame: {duration: 1}}\")\n", " ```\n", "\n" ] } ], "source": [ "import plotly\n", "help(plotly.offline.iplot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more examples on plotting offline with Plotly in python please visit our [offline documentation](https://plotly.com/python/offline/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using Plotly with Pandas\n", "\n", "To use Plotly with Pandas first `$ pip install pandas` and then import pandas in your code like in the example below." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import pandas as pd\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')\n", "\n", "fig = {\n", " 'data': [\n", " {\n", " 'x': df.gdpPercap, \n", " 'y': df.lifeExp, \n", " 'text': df.country, \n", " 'mode': 'markers', \n", " 'name': '2007'},\n", " ],\n", " 'layout': {\n", " 'xaxis': {'title': 'GDP per Capita', 'type': 'log'},\n", " 'yaxis': {'title': \"Life Expectancy\"}\n", " }\n", "}\n", "\n", "py.iplot(fig, filename='pandas-multiple-scatter')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [MORE EXAMPLES](https://plotly.com/python/)\n", "Check out more examples and tutorials for using Plotly in python [here](https://plotly.com/python)!" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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", " 'getting-started.ipynb', 'python/getting-started/', 'Getting Started Plotly for Python',\n", " 'Installation and Initialization Steps for Using Plotly in Python.',\n", " title = 'Getting Started with Plotly for Python | plotly',\n", " name = 'Getting Started with Plotly for Python', display_as='chart_studio'\n", " language='python', layout='user-guide', has_thumbnail='true', thumbnail='thumbnail/bubble.jpg',\n", " ipynb= '~notebook_demo/123/installation', uses_plotly_offline=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }