{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/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.6.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### US Flight Paths Map" ] }, { "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", "import pandas as pd\n", "\n", "df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')\n", "df_airports.head()\n", "\n", "df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')\n", "df_flight_paths.head()\n", "\n", "airports = [go.Scattergeo(\n", " locationmode = 'USA-states',\n", " lon = df_airports['long'],\n", " lat = df_airports['lat'],\n", " hoverinfo = 'text',\n", " text = df_airports['airport'],\n", " mode = 'markers',\n", " marker = go.scattergeo.Marker(\n", " size = 2,\n", " color = 'rgb(255, 0, 0)',\n", " line = go.scattergeo.marker.Line(\n", " width = 3,\n", " color = 'rgba(68, 68, 68, 0)'\n", " )\n", " ))]\n", "\n", "flight_paths = []\n", "for i in range(len(df_flight_paths)):\n", " flight_paths.append(\n", " go.Scattergeo(\n", " locationmode = 'USA-states',\n", " lon = [df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i]],\n", " lat = [df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i]],\n", " mode = 'lines',\n", " line = go.scattergeo.Line(\n", " width = 1,\n", " color = 'red',\n", " ),\n", " opacity = float(df_flight_paths['cnt'][i]) / float(df_flight_paths['cnt'].max()),\n", " )\n", " )\n", "\n", "layout = go.Layout(\n", " title = go.layout.Title(\n", " text = 'Feb. 2011 American Airline flight paths
(Hover for airport names)'\n", " ),\n", " showlegend = False,\n", " geo = go.layout.Geo(\n", " scope = 'north america',\n", " projection = go.layout.geo.Projection(type = 'azimuthal equal area'),\n", " showland = True,\n", " landcolor = 'rgb(243, 243, 243)',\n", " countrycolor = 'rgb(204, 204, 204)',\n", " ),\n", ")\n", "\n", "fig = go.Figure(data = flight_paths + airports, layout = layout)\n", "py.iplot(fig, filename = 'd3-flight-paths')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### London to NYC Great Circle" ] }, { "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", "nyc_london = [go.Scattergeo(\n", " lat = [40.7127, 51.5072],\n", " lon = [-74.0059, 0.1275],\n", " mode = 'lines',\n", " line = go.scattergeo.Line(\n", " width = 2,\n", " color = 'blue',\n", " ),\n", ")]\n", "\n", "layout = go.Layout(\n", " title = go.layout.Title(\n", " text = 'London to NYC Great Circle'\n", " ),\n", " showlegend = False,\n", " geo = go.layout.Geo(\n", " resolution = 50,\n", " showland = True,\n", " showlakes = True,\n", " landcolor = 'rgb(204, 204, 204)',\n", " countrycolor = 'rgb(204, 204, 204)',\n", " lakecolor = 'rgb(255, 255, 255)',\n", " projection = go.layout.geo.Projection(\n", " type = \"equirectangular\"\n", " ),\n", " coastlinewidth = 2,\n", " lataxis = go.layout.geo.Lataxis(\n", " range = [20, 60],\n", " showgrid = True,\n", " dtick = 10\n", " ),\n", " lonaxis = go.layout.geo.Lonaxis(\n", " range = [-100, 20],\n", " showgrid = True,\n", " dtick = 20\n", " ),\n", " )\n", ")\n", "\n", "fig = go.Figure(data = nyc_london, layout = layout)\n", "py.iplot(fig, filename = 'd3-great-circle')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Contour lines on globe" ] }, { "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", "\n", "import pandas as pd\n", "try:\n", " from itertools import izip# Python 2\n", "except ImportError:\n", " izip = zip# Python 3\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/globe_contours.csv')\n", "df.head()\n", "\n", "contours = []\n", "\n", "scl = ['rgb(213,62,79)', 'rgb(244,109,67)', 'rgb(253,174,97)', \\\n", " 'rgb(254,224,139)', 'rgb(255,255,191)', 'rgb(230,245,152)', \\\n", " 'rgb(171,221,164)', 'rgb(102,194,165)', 'rgb(50,136,189)'\n", "]\n", "\n", "def pairwise(iterable):\n", " a = iter(iterable)\n", " return izip(a, a)\n", "\n", "i = 0\n", "for lat, lon in pairwise(df.columns):\n", " contours.append(go.Scattergeo(\n", " lon = df[lon],\n", " lat = df[lat],\n", " mode = 'lines',\n", " line = go.scattergeo.Line(\n", " width = 2,\n", " color = scl[i]\n", " )))\n", " i = 0 if i + 1 >= len(df.columns) / 4 else i + 1\n", "\n", "layout = go.Layout(\n", " title = go.layout.Title(\n", " text = 'Contour lines over globe
(Click and drag to rotate)'\n", " ),\n", " showlegend = False,\n", " geo = go.layout.Geo(\n", " showland = True,\n", " showlakes = True,\n", " showcountries = True,\n", " showocean = True,\n", " countrywidth = 0.5,\n", " landcolor = 'rgb(230, 145, 56)',\n", " lakecolor = 'rgb(0, 255, 255)',\n", " oceancolor = 'rgb(0, 255, 255)',\n", " projection = go.layout.geo.Projection(\n", " type = 'orthographic',\n", " rotation = go.layout.geo.projection.Rotation(\n", " lon = -100,\n", " lat = 40,\n", " roll = 0\n", " )\n", " ),\n", " lonaxis = go.layout.geo.Lonaxis(\n", " showgrid = True,\n", " gridcolor = 'rgb(102, 102, 102)',\n", " gridwidth = 0.5\n", " ),\n", " lataxis = go.layout.geo.Lataxis(\n", " showgrid = True,\n", " gridcolor = 'rgb(102, 102, 102)',\n", " gridwidth = 0.5\n", " )\n", " )\n", ")\n", "\n", "fig = go.Figure(data = contours, layout = layout)\n", "py.iplot(fig, filename = 'd3-globe')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Reference\n", "See https://plot.ly/python/reference/#scattergeo for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "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 c:\\users\\priyat~1\\appdata\\local\\temp\\pip-req-build-j78_c14c\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher: started\n", " Running setup.py bdist_wheel for publisher: finished with status 'done'\n", " Stored in directory: C:\\Users\\PRIYAT~1\\AppData\\Local\\Temp\\pip-ephem-wheel-cache-34r8gj90\\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" ] } ], "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", " 'lines_on_maps.ipynb', 'python/lines-on-maps/', 'Lines on maps | plotly',\n", " 'How to draw lines, great circles, and contours on maps in Python.',\n", " title = 'Lines on maps | plotly',\n", " name = 'Lines on Maps',\n", " has_thumbnail='true', thumbnail='thumbnail/flight-paths.jpg', \n", " language='python', page_type='example_index',\n", " display_as='maps', order=4,\n", " ipynb= '~notebook_demo/58')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.1" } }, "nbformat": 4, "nbformat_minor": 1 }