{
"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!\n",
"#### 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": [
"'2.7.0'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Simple Scatter Plot"
]
},
{
"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",
"# Create random data with numpy\n",
"import numpy as np\n",
"\n",
"N = 1000\n",
"random_x = np.random.randn(N)\n",
"random_y = np.random.randn(N)\n",
"\n",
"# Create a trace\n",
"trace = go.Scatter(\n",
" x = random_x,\n",
" y = random_y,\n",
" mode = 'markers'\n",
")\n",
"\n",
"data = [trace]\n",
"\n",
"# Plot and embed in ipython notebook!\n",
"py.iplot(data, filename='basic-scatter')\n",
"\n",
"# or plot with: plot_url = py.plot(data, filename='basic-line')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"#### Line and Scatter Plots"
]
},
{
"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",
"# Create random data with numpy\n",
"import numpy as np\n",
"\n",
"N = 100\n",
"random_x = np.linspace(0, 1, N)\n",
"random_y0 = np.random.randn(N)+5\n",
"random_y1 = np.random.randn(N)\n",
"random_y2 = np.random.randn(N)-5\n",
"\n",
"# Create traces\n",
"trace0 = go.Scatter(\n",
" x = random_x,\n",
" y = random_y0,\n",
" mode = 'markers',\n",
" name = 'markers'\n",
")\n",
"trace1 = go.Scatter(\n",
" x = random_x,\n",
" y = random_y1,\n",
" mode = 'lines+markers',\n",
" name = 'lines+markers'\n",
")\n",
"trace2 = go.Scatter(\n",
" x = random_x,\n",
" y = random_y2,\n",
" mode = 'lines',\n",
" name = 'lines'\n",
")\n",
"\n",
"data = [trace0, trace1, trace2]\n",
"py.iplot(data, filename='scatter-mode')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"#### Style Scatter Plots"
]
},
{
"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 numpy as np\n",
"\n",
"N = 500\n",
"\n",
"trace0 = go.Scatter(\n",
" x = np.random.randn(N),\n",
" y = np.random.randn(N)+2,\n",
" name = 'Above',\n",
" mode = 'markers',\n",
" marker = dict(\n",
" size = 10,\n",
" color = 'rgba(152, 0, 0, .8)',\n",
" line = dict(\n",
" width = 2,\n",
" color = 'rgb(0, 0, 0)'\n",
" )\n",
" )\n",
")\n",
"\n",
"trace1 = go.Scatter(\n",
" x = np.random.randn(N),\n",
" y = np.random.randn(N)-2,\n",
" name = 'Below',\n",
" mode = 'markers',\n",
" marker = dict(\n",
" size = 10,\n",
" color = 'rgba(255, 182, 193, .9)',\n",
" line = dict(\n",
" width = 2,\n",
" )\n",
" )\n",
")\n",
"\n",
"data = [trace0, trace1]\n",
"\n",
"layout = dict(title = 'Styled Scatter',\n",
" yaxis = dict(zeroline = False),\n",
" xaxis = dict(zeroline = False)\n",
" )\n",
"\n",
"fig = dict(data=data, layout=layout)\n",
"py.iplot(fig, filename='styled-scatter')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"#### Data Labels on Hover"
]
},
{
"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 random\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"l= []\n",
"y= []\n",
"data= pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv\")\n",
"# Setting colors for plot.\n",
"N= 53\n",
"c= ['hsl('+str(h)+',50%'+',50%)' for h in np.linspace(0, 360, N)]\n",
"\n",
"for i in range(int(N)):\n",
" y.append((2000+i))\n",
" trace0= go.Scatter(\n",
" x= data['Rank'],\n",
" y= data['Population']+(i*1000000),\n",
" mode= 'markers',\n",
" marker= dict(size= 14,\n",
" line= dict(width=1),\n",
" color= c[i],\n",
" opacity= 0.3\n",
" ),name= y[i],\n",
" text= data['State']) # The hover text goes here... \n",
" l.append(trace0);\n",
"\n",
"layout= go.Layout(\n",
" title= 'Stats of USA States',\n",
" hovermode= 'closest',\n",
" xaxis= dict(\n",
" title= 'Population',\n",
" ticklen= 5,\n",
" zeroline= False,\n",
" gridwidth= 2,\n",
" ),\n",
" yaxis=dict(\n",
" title= 'Rank',\n",
" ticklen= 5,\n",
" gridwidth= 2,\n",
" ),\n",
" showlegend= False\n",
")\n",
"fig= go.Figure(data=l, layout=layout)\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Scatter with a Color Dimension"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.graph_objs as go\n",
"import plotly.plotly as py\n",
"\n",
"import numpy as np\n",
"\n",
"trace1 = go.Scatter(\n",
" y = np.random.randn(500),\n",
" mode='markers',\n",
" marker=dict(\n",
" size=16,\n",
" color = np.random.randn(500), #set color equal to a variable\n",
" colorscale='Viridis',\n",
" showscale=True\n",
" )\n",
")\n",
"data = [trace1]\n",
"\n",
"py.iplot(data, filename='scatter-plot-with-colorscale')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Large Data Sets\n",
"\n",
"Now in Ploty you can implement WebGL with `Scattergl()` in place of `Scatter()`
\n",
"for increased speed, improved interactivity, and the ability to plot even more data!"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"import numpy as np\n",
"\n",
"N = 100000\n",
"trace = go.Scattergl(\n",
" x = np.random.randn(N),\n",
" y = np.random.randn(N),\n",
" mode = 'markers',\n",
" marker = dict(\n",
" color = '#FFBAD2',\n",
" line = dict(width = 1)\n",
" )\n",
")\n",
"data = [trace]\n",
"py.iplot(data, filename='compare_webgl')"
]
},
{
"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-linescatterplot) can easily be deployed to a PaaS."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-linescatterplot/\", width=\"100%\",height=\"750px\", frameBorder=\"0\")\n"
]
},
{
"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-linescatterplot/code\", width=\"100%\",height=500, frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference\n",
"See https://plotly.com/python/reference/#scatter or https://plotly.com/python/reference/#scattergl for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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 /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-j9bh86ca\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-vv2wsm5d/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",
" \n",
"import publisher\n",
"publisher.publish(\n",
" 'scatter.ipynb', 'python/line-and-scatter/', 'Python Scatter Plots | plotly',\n",
" 'How to make scatter plots in Python with Plotly.',\n",
" title = 'Python Scatter Plots | plotly',\n",
" name = 'Scatter Plots',\n",
" has_thumbnail='true', thumbnail='thumbnail/line-and-scatter.jpg', \n",
" language='python', page_type='example_index',\n",
" display_as='basic', order=2,\n",
" redirect_from='python/line-and-scatter-plots-tutorial/',\n",
" ipynb= '~notebook_demo/2')"
]
}
],
"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": 1
}