{
"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": [
"#### Simple Bubble Chart"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"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",
"# Get Data: this ex will only use part of it (i.e. rows 750-1500)\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')\n",
"\n",
"trace1 = go.Scatter3d(\n",
" x=df['year'][750:1500],\n",
" y=df['continent'][750:1500],\n",
" z=df['pop'][750:1500],\n",
" text=df['country'][750:1500],\n",
" mode='markers',\n",
" marker=dict(\n",
" sizemode='diameter',\n",
" sizeref=750,\n",
" size=df['gdpPercap'][750:1500],\n",
" color = df['lifeExp'][750:1500],\n",
" colorscale = 'Viridis',\n",
" colorbar = dict(title = 'Life
Expectancy'),\n",
" line=dict(color='rgb(140, 140, 170)')\n",
" )\n",
")\n",
"\n",
"data=[trace1]\n",
"\n",
"layout=go.Layout(height=800, width=800, title='Examining Population and Life Expectancy Over Time')\n",
"\n",
"fig=go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='3DBubble')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Bubble Chart Sized by a Variable\n",
"Plot planets' distance from sun, density, and gravity with bubble size based on planet size"
]
},
{
"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",
"planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']\n",
"planet_colors = ['rgb(135, 135, 125)', 'rgb(210, 50, 0)', 'rgb(50, 90, 255)',\n",
" 'rgb(178, 0, 0)', 'rgb(235, 235, 210)', 'rgb(235, 205, 130)',\n",
" 'rgb(55, 255, 217)', 'rgb(38, 0, 171)', 'rgb(255, 255, 255)']\n",
"distance_from_sun = [57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1, 5906.4]\n",
"density = [5427, 5243, 5514, 3933, 1326, 687, 1271, 1638, 2095]\n",
"gravity = [3.7, 8.9, 9.8, 3.7, 23.1, 9.0, 8.7, 11.0, 0.7]\n",
"planet_diameter = [4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528, 2370]\n",
"\n",
"# Create trace, sizing bubbles by planet diameter\n",
"trace1 = go.Scatter3d(\n",
" x = distance_from_sun,\n",
" y = density,\n",
" z = gravity,\n",
" text = planets,\n",
" mode = 'markers',\n",
" marker = dict(\n",
" sizemode = 'diameter',\n",
" sizeref = 750, # info on sizeref: https://plotly.com/python/reference/#scatter-marker-sizeref\n",
" size = planet_diameter,\n",
" color = planet_colors,\n",
" ) \n",
")\n",
"data=[trace1]\n",
"\n",
"layout=go.Layout(width=800, height=800, title = 'Planets!',\n",
" scene = dict(xaxis=dict(title='Distance from Sun',\n",
" titlefont=dict(color='Orange')),\n",
" yaxis=dict(title='Density',\n",
" titlefont=dict(color='rgb(220, 220, 220)')),\n",
" zaxis=dict(title='Gravity',\n",
" titlefont=dict(color='rgb(220, 220, 220)')),\n",
" bgcolor = 'rgb(20, 24, 54)'\n",
" )\n",
" )\n",
"\n",
"fig=go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='solar_system_planet_size')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Edit the Colorbar\n",
"Plot planets' distance from sun, density, and gravity with bubble size based on planet size"
]
},
{
"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",
"planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']\n",
"temperatures = [167, 464, 15, -20, -65, -110, -140, -195, -200, -225]\n",
"distance_from_sun = [57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1, 5906.4]\n",
"density = [5427, 5243, 5514, 3933, 1326, 687, 1271, 1638, 2095]\n",
"gravity = [3.7, 8.9, 9.8, 3.7, 23.1, 9.0, 8.7, 11.0, 0.7]\n",
"planet_diameter = [4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528, 2370]\n",
"\n",
"# Create trace, sizing bubbles by planet diameter\n",
"trace1 = go.Scatter3d(\n",
" x = distance_from_sun,\n",
" y = density,\n",
" z = gravity,\n",
" text = planets,\n",
" mode = 'markers',\n",
" marker = dict(\n",
" sizemode = 'diameter',\n",
" sizeref = 750, # info on sizeref: https://plotly.com/python/reference/#scatter-marker-sizeref\n",
" size = planet_diameter,\n",
" color = temperatures,\n",
" colorbar = dict(title = 'Mean
Temperature'),\n",
" colorscale=[[0, 'rgb(5, 10, 172)'], [.3, 'rgb(255, 255, 255)'], [1, 'rgb(178, 10, 28)']]\n",
" ) \n",
")\n",
"data=[trace1]\n",
"\n",
"layout=go.Layout(width=800, height=800, title = 'Planets!!',\n",
" scene = dict(xaxis=dict(title='Distance from Sun',\n",
" titlefont=dict(color='Orange')),\n",
" yaxis=dict(title='Density',\n",
" titlefont=dict(color='rgb(220, 220, 220)')),\n",
" zaxis=dict(title='Gravity',\n",
" titlefont=dict(color='rgb(220, 220, 220)')),\n",
" bgcolor = 'rgb(20, 24, 54)'\n",
" )\n",
" )\n",
"\n",
"fig=go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='solar_system_planet_temp')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#scatter3d and https://plotly.com/python/reference/#scatter-marker-sizeref
for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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 c:\\users\\brand\\appdata\\local\\temp\\pip-req-build-ju5537hh\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": [
"You are using pip version 10.0.0, however version 10.0.1 is available.\n",
"You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n",
"C:\\Python27\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\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",
" '3dbubble.ipynb', 'python/3d-bubble-charts/', 'Python 3D Bubble Charts',\n",
" 'How to make 3D Bubble Charts in Python with Plotly. '\n",
" 'Three examples of 3D Bubble Charts.',\n",
" title = 'Python 3D Bubble Charts | plotly',\n",
" name = '3D Bubble Charts',\n",
" has_thumbnail='true', thumbnail='thumbnail/3dbubble.jpg', \n",
" language='python', display_as='3d_charts', order=2,\n",
" ipynb= '~notebook_demo/62')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"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
}