Skip to content

Commit bd66ca0

Browse files
authored
3d tutorials: surface coloring, camera controls, bubble chart
* various 3d tutorials * 3d line plot * px in 3d-line-plots * camera controls: better tutorial
1 parent 55ed0fb commit bd66ca0

8 files changed

+662
-718
lines changed

python/3d-bubble-charts.md

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.1'
9+
jupytext_version: 1.2.3
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.7.3
24+
plotly:
25+
description: How to make 3D Bubble Charts in Python with Plotly. Three examples
26+
of 3D Bubble Charts.
27+
display_as: 3d_charts
28+
has_thumbnail: true
29+
ipynb: ~notebook_demo/62
30+
language: python
31+
layout: user-guide
32+
name: 3D Bubble Charts
33+
order: 2
34+
page_type: u-guide
35+
permalink: python/3d-bubble-charts/
36+
thumbnail: thumbnail/3dbubble.jpg
37+
title: Python 3D Bubble Charts | plotly
38+
---
39+
40+
### 3d Bubble chart with plotly express
41+
42+
```python
43+
import plotly.express as px
44+
import numpy as np
45+
gapminder = px.data.gapminder()
46+
#gapminder.columns
47+
fig = px.scatter_3d(gapminder, x='year', y='continent', z='pop', size='gdpPercap', color='lifeExp',
48+
hover_data=['country'])
49+
fig.update_layout(scene_zaxis_type="log")
50+
fig.show()
51+
```
52+
53+
54+
#### Simple Bubble Chart
55+
56+
```python
57+
import plotly.graph_objects as go
58+
59+
import pandas as pd
60+
61+
# Get Data: this ex will only use part of it (i.e. rows 750-1500)
62+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
63+
64+
start, end = 750, 1500
65+
66+
fig = go.Figure(data=go.Scatter3d(
67+
x=df['year'][start:end],
68+
y=df['continent'][start:end],
69+
z=df['pop'][start:end],
70+
text=df['country'][start:end],
71+
mode='markers',
72+
marker=dict(
73+
sizemode='diameter',
74+
sizeref=750,
75+
size=df['gdpPercap'][start:end],
76+
color = df['lifeExp'][start:end],
77+
colorscale = 'Viridis',
78+
colorbar_title = 'Life<br>Expectancy',
79+
line_color='rgb(140, 140, 170)'
80+
)
81+
))
82+
83+
84+
fig.update_layout(height=800, width=800,
85+
title='Examining Population and Life Expectancy Over Time')
86+
87+
fig.show()
88+
```
89+
90+
#### Bubble Chart Sized by a Variable
91+
Plot planets' distance from sun, density, and gravity with bubble size based on planet size
92+
93+
```python
94+
import plotly.graph_objects as go
95+
96+
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
97+
planet_colors = ['rgb(135, 135, 125)', 'rgb(210, 50, 0)', 'rgb(50, 90, 255)',
98+
'rgb(178, 0, 0)', 'rgb(235, 235, 210)', 'rgb(235, 205, 130)',
99+
'rgb(55, 255, 217)', 'rgb(38, 0, 171)', 'rgb(255, 255, 255)']
100+
distance_from_sun = [57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1, 5906.4]
101+
density = [5427, 5243, 5514, 3933, 1326, 687, 1271, 1638, 2095]
102+
gravity = [3.7, 8.9, 9.8, 3.7, 23.1, 9.0, 8.7, 11.0, 0.7]
103+
planet_diameter = [4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528, 2370]
104+
105+
# Create trace, sizing bubbles by planet diameter
106+
fig = go.Figure(data=go.Scatter3d(
107+
x = distance_from_sun,
108+
y = density,
109+
z = gravity,
110+
text = planets,
111+
mode = 'markers',
112+
marker = dict(
113+
sizemode = 'diameter',
114+
sizeref = 750, # info on sizeref: https://plot.ly/python/reference/#scatter-marker-sizeref
115+
size = planet_diameter,
116+
color = planet_colors,
117+
)
118+
))
119+
120+
fig.update_layout(width=800, height=800, title = 'Planets!',
121+
scene = dict(xaxis=dict(title='Distance from Sun', titlefont_color='white'),
122+
yaxis=dict(title='Density', titlefont_color='white'),
123+
zaxis=dict(title='Gravity', titlefont_color='white'),
124+
bgcolor = 'rgb(20, 24, 54)'
125+
))
126+
127+
fig.show()
128+
```
129+
130+
#### Edit the Colorbar
131+
Plot planets' distance from sun, density, and gravity with bubble size based on planet size
132+
133+
```python
134+
import plotly.graph_objects as go
135+
136+
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
137+
temperatures = [167, 464, 15, -20, -65, -110, -140, -195, -200, -225]
138+
distance_from_sun = [57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1, 5906.4]
139+
density = [5427, 5243, 5514, 3933, 1326, 687, 1271, 1638, 2095]
140+
gravity = [3.7, 8.9, 9.8, 3.7, 23.1, 9.0, 8.7, 11.0, 0.7]
141+
planet_diameter = [4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528, 2370]
142+
143+
# Create trace, sizing bubbles by planet diameter
144+
fig = go.Figure(go.Scatter3d(
145+
x = distance_from_sun,
146+
y = density,
147+
z = gravity,
148+
text = planets,
149+
mode = 'markers',
150+
marker = dict(
151+
sizemode = 'diameter',
152+
sizeref = 750, # info on sizeref: https://plot.ly/python/reference/#scatter-marker-sizeref
153+
size = planet_diameter,
154+
color = temperatures,
155+
colorbar_title = 'Mean<br>Temperature',
156+
colorscale=[[0, 'rgb(5, 10, 172)'], [.3, 'rgb(255, 255, 255)'], [1, 'rgb(178, 10, 28)']]
157+
)
158+
))
159+
160+
fig.update_layout(width=800, height=800, title = 'Planets!',
161+
scene = dict(xaxis=dict(title='Distance from Sun', titlefont_color='white'),
162+
yaxis=dict(title='Density', titlefont_color='white'),
163+
zaxis=dict(title='Gravity', titlefont_color='white'),
164+
bgcolor = 'rgb(20, 24, 54)'
165+
))
166+
167+
fig.show()
168+
```
169+
170+
#### Reference
171+
See https://plot.ly/python/reference/#scatter3d and https://plot.ly/python/reference/#scatter-marker-sizeref <br>for more information and chart attribute options!
172+

0 commit comments

Comments
 (0)