#!/usr/bin/env python
# coding: utf-8
# #### New to Plotly?
# 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/).
#
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).
#
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
# #### Simple Bubble Chart
# In[1]:
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
# Get Data: this ex will only use part of it (i.e. rows 750-1500)
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
trace1 = go.Scatter3d(
x=df['year'][750:1500],
y=df['continent'][750:1500],
z=df['pop'][750:1500],
text=df['country'][750:1500],
mode='markers',
marker=dict(
sizemode='diameter',
sizeref=750,
size=df['gdpPercap'][750:1500],
color = df['lifeExp'][750:1500],
colorscale = 'Viridis',
colorbar = dict(title = 'Life
Expectancy'),
line=dict(color='rgb(140, 140, 170)')
)
)
data=[trace1]
layout=go.Layout(height=800, width=800, title='Examining Population and Life Expectancy Over Time')
fig=go.Figure(data=data, layout=layout)
py.iplot(fig, filename='3DBubble')
# #### Bubble Chart Sized by a Variable
# Plot planets' distance from sun, density, and gravity with bubble size based on planet size
# In[2]:
import plotly.plotly as py
import plotly.graph_objs as go
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
planet_colors = ['rgb(135, 135, 125)', 'rgb(210, 50, 0)', 'rgb(50, 90, 255)',
'rgb(178, 0, 0)', 'rgb(235, 235, 210)', 'rgb(235, 205, 130)',
'rgb(55, 255, 217)', 'rgb(38, 0, 171)', 'rgb(255, 255, 255)']
distance_from_sun = [57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1, 5906.4]
density = [5427, 5243, 5514, 3933, 1326, 687, 1271, 1638, 2095]
gravity = [3.7, 8.9, 9.8, 3.7, 23.1, 9.0, 8.7, 11.0, 0.7]
planet_diameter = [4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528, 2370]
# Create trace, sizing bubbles by planet diameter
trace1 = go.Scatter3d(
x = distance_from_sun,
y = density,
z = gravity,
text = planets,
mode = 'markers',
marker = dict(
sizemode = 'diameter',
sizeref = 750, # info on sizeref: https://plotly.com/python/reference/#scatter-marker-sizeref
size = planet_diameter,
color = planet_colors,
)
)
data=[trace1]
layout=go.Layout(width=800, height=800, title = 'Planets!',
scene = dict(xaxis=dict(title='Distance from Sun',
titlefont=dict(color='Orange')),
yaxis=dict(title='Density',
titlefont=dict(color='rgb(220, 220, 220)')),
zaxis=dict(title='Gravity',
titlefont=dict(color='rgb(220, 220, 220)')),
bgcolor = 'rgb(20, 24, 54)'
)
)
fig=go.Figure(data=data, layout=layout)
py.iplot(fig, filename='solar_system_planet_size')
# #### Edit the Colorbar
# Plot planets' distance from sun, density, and gravity with bubble size based on planet size
# In[3]:
import plotly.plotly as py
import plotly.graph_objs as go
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
temperatures = [167, 464, 15, -20, -65, -110, -140, -195, -200, -225]
distance_from_sun = [57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1, 5906.4]
density = [5427, 5243, 5514, 3933, 1326, 687, 1271, 1638, 2095]
gravity = [3.7, 8.9, 9.8, 3.7, 23.1, 9.0, 8.7, 11.0, 0.7]
planet_diameter = [4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528, 2370]
# Create trace, sizing bubbles by planet diameter
trace1 = go.Scatter3d(
x = distance_from_sun,
y = density,
z = gravity,
text = planets,
mode = 'markers',
marker = dict(
sizemode = 'diameter',
sizeref = 750, # info on sizeref: https://plotly.com/python/reference/#scatter-marker-sizeref
size = planet_diameter,
color = temperatures,
colorbar = dict(title = 'Mean
Temperature'),
colorscale=[[0, 'rgb(5, 10, 172)'], [.3, 'rgb(255, 255, 255)'], [1, 'rgb(178, 10, 28)']]
)
)
data=[trace1]
layout=go.Layout(width=800, height=800, title = 'Planets!!',
scene = dict(xaxis=dict(title='Distance from Sun',
titlefont=dict(color='Orange')),
yaxis=dict(title='Density',
titlefont=dict(color='rgb(220, 220, 220)')),
zaxis=dict(title='Gravity',
titlefont=dict(color='rgb(220, 220, 220)')),
bgcolor = 'rgb(20, 24, 54)'
)
)
fig=go.Figure(data=data, layout=layout)
py.iplot(fig, filename='solar_system_planet_temp')
# #### Reference
# See https://plotly.com/python/reference/#scatter3d and https://plotly.com/python/reference/#scatter-marker-sizeref
for more information and chart attribute options!
# In[4]:
from IPython.display import display, HTML
display(HTML(''))
display(HTML(''))
get_ipython().system(' pip install git+https://github.com/plotly/publisher.git --upgrade')
import publisher
publisher.publish(
'3dbubble.ipynb', 'python/3d-bubble-charts/', 'Python 3D Bubble Charts',
'How to make 3D Bubble Charts in Python with Plotly. '
'Three examples of 3D Bubble Charts.',
title = 'Python 3D Bubble Charts | plotly',
name = '3D Bubble Charts',
has_thumbnail='true', thumbnail='thumbnail/3dbubble.jpg',
language='python', display_as='3d_charts', order=2,
ipynb= '~notebook_demo/62')
# In[ ]: