Skip to content

Commit 37284c3

Browse files
committed
bubble-charts notebook
1 parent 7d0bd9f commit 37284c3

File tree

1 file changed

+44
-93
lines changed

1 file changed

+44
-93
lines changed

notebooks/bubble-charts.md

Lines changed: 44 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
---
22
jupyter:
33
jupytext:
4+
notebook_metadata_filter: all
45
text_representation:
56
extension: .md
67
format_name: markdown
78
format_version: '1.1'
89
jupytext_version: 1.1.1
910
kernelspec:
10-
display_name: Python 2
11+
display_name: Python 3
1112
language: python
12-
name: python2
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.6.7
1324
plotly:
1425
description: How to make bubble charts in Python with Plotly.
1526
display_as: basic
@@ -26,46 +37,28 @@ jupyter:
2637
title: Bubble Charts | plotly
2738
---
2839

29-
#### New to Plotly?
30-
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/).
31-
<br>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).
32-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
33-
#### Version Check
34-
Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version.
35-
36-
```python
37-
import plotly
38-
plotly.__version__
39-
```
40-
4140
### Simple Bubble Chart
4241

4342
```python
44-
import plotly.plotly as py
4543
import plotly.graph_objs as go
4644

4745
trace0 = go.Scatter(
48-
x=[1, 2, 3, 4],
49-
y=[10, 11, 12, 13],
46+
x=[1, 2, 3, 4], y=[10, 11, 12, 13],
5047
mode='markers',
51-
marker=dict(
52-
size=[40, 60, 80, 100],
53-
)
48+
marker_size=[40, 60, 80, 100]
5449
)
5550

56-
data = [trace0]
57-
py.iplot(data, filename='bubblechart-size')
51+
fig = go.Figure(data=[trace0])
52+
fig.show()
5853
```
5954

6055
### Setting Marker Size and Color
6156

6257
```python
63-
import plotly.plotly as py
6458
import plotly.graph_objs as go
6559

6660
trace0 = go.Scatter(
67-
x=[1, 2, 3, 4],
68-
y=[10, 11, 12, 13],
61+
x=[1, 2, 3, 4], y=[10, 11, 12, 13],
6962
mode='markers',
7063
marker=dict(
7164
color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)',
@@ -75,8 +68,8 @@ trace0 = go.Scatter(
7568
)
7669
)
7770

78-
data = [trace0]
79-
py.iplot(data, filename='bubblechart-color')
71+
fig = go.Figure(data = [trace0])
72+
fig.show()
8073
```
8174

8275
### Scaling the Size of Bubble Charts
@@ -86,7 +79,6 @@ Note that setting 'sizeref' to a value greater than 1, decreases the rendered ma
8679
Additionally, we recommend setting the sizemode attribute: https://plot.ly/python/reference/#scatter-marker-sizemode to area.
8780

8881
```python
89-
import plotly.plotly as py
9082
import plotly.graph_objs as go
9183

9284
size = [20, 40, 60, 80, 100, 80, 60, 40, 20, 40]
@@ -102,19 +94,17 @@ trace0 = go.Scatter(
10294
)
10395
)
10496

105-
data = [trace0]
106-
py.iplot(data, filename='bubblechart-size-ref')
97+
fig = go.Figure(data=[trace0])
98+
fig.show()
10799
```
108100

109101
### Hover Text with Bubble Charts
110102

111103
```python
112-
import plotly.plotly as py
113104
import plotly.graph_objs as go
114105

115106
trace0 = go.Scatter(
116-
x=[1, 2, 3, 4],
117-
y=[10, 11, 12, 13],
107+
x=[1, 2, 3, 4], y=[10, 11, 12, 13],
118108
text=['A<br>size: 40', 'B<br>size: 60', 'C<br>size: 80', 'D<br>size: 100'],
119109
mode='markers',
120110
marker=dict(
@@ -123,38 +113,33 @@ trace0 = go.Scatter(
123113
)
124114
)
125115

126-
data = [trace0]
127-
py.iplot(data, filename='bubblechart-text')
116+
fig = go.Figure(data=[trace0])
117+
fig.show()
128118
```
129119

130120
### Bubble Charts with Colorscale
131121

132122
```python
133-
import plotly.plotly as py
134123
import plotly.graph_objs as go
135124

136-
data = [
137-
{
138-
'x': [1, 3.2, 5.4, 7.6, 9.8, 12.5],
139-
'y': [1, 3.2, 5.4, 7.6, 9.8, 12.5],
140-
'mode': 'markers',
141-
'marker': {
142-
'color': [120, 125, 130, 135, 140, 145],
143-
'size': [15, 30, 55, 70, 90, 110],
144-
'showscale': True
145-
}
146-
}
147-
]
148-
149-
py.iplot(data, filename='scatter-colorscale')
125+
trace = go.Scatter(
126+
x=[1, 3.2, 5.4, 7.6, 9.8, 12.5],
127+
y=[1, 3.2, 5.4, 7.6, 9.8, 12.5],
128+
mode='markers',
129+
marker=dict(
130+
color=[120, 125, 130, 135, 140, 145],
131+
size=[15, 30, 55, 70, 90, 110],
132+
showscale=True
133+
)
134+
)
135+
fig = go.Figure(data=[trace])
136+
fig.show()
150137
```
151138

152139
### Categorical Bubble Charts
153140

154141
```python
155-
import plotly.plotly as py
156142
import plotly.graph_objs as go
157-
158143
import pandas as pd
159144
import math
160145

@@ -192,9 +177,7 @@ trace0 = go.Scatter(
192177
sizemode='area',
193178
sizeref=sizeref,
194179
size=df_2007['size'][df_2007['continent'] == 'Africa'],
195-
line=dict(
196-
width=2
197-
),
180+
line_width=2
198181
)
199182
)
200183
trace1 = go.Scatter(
@@ -207,9 +190,7 @@ trace1 = go.Scatter(
207190
sizemode='area',
208191
sizeref=sizeref,
209192
size=df_2007['size'][df_2007['continent'] == 'Americas'],
210-
line=dict(
211-
width=2
212-
),
193+
line_width=2
213194
)
214195
)
215196
trace2 = go.Scatter(
@@ -222,9 +203,7 @@ trace2 = go.Scatter(
222203
sizemode='area',
223204
sizeref=sizeref,
224205
size=df_2007['size'][df_2007['continent'] == 'Asia'],
225-
line=dict(
226-
width=2
227-
),
206+
line_width=2
228207
)
229208
)
230209
trace3 = go.Scatter(
@@ -237,9 +216,7 @@ trace3 = go.Scatter(
237216
sizemode='area',
238217
sizeref=sizeref,
239218
size=df_2007['size'][df_2007['continent'] == 'Europe'],
240-
line=dict(
241-
width=2
242-
),
219+
line_width=2
243220
)
244221
)
245222
trace4 = go.Scatter(
@@ -252,13 +229,10 @@ trace4 = go.Scatter(
252229
sizemode='area',
253230
sizeref=sizeref,
254231
size=df_2007['size'][df_2007['continent'] == 'Oceania'],
255-
line=dict(
256-
width=2
257-
),
232+
line_width=2,
258233
)
259234
)
260235

261-
data = [trace0, trace1, trace2, trace3, trace4]
262236
layout = go.Layout(
263237
title='Life Expectancy v. Per Capita GDP, 2007',
264238
xaxis=dict(
@@ -282,33 +256,10 @@ layout = go.Layout(
282256
plot_bgcolor='rgb(243, 243, 243)',
283257
)
284258

285-
fig = go.Figure(data=data, layout=layout)
286-
py.iplot(fig, filename='life-expectancy-per-GDP-2007')
259+
fig = go.Figure(data=[trace0, trace1, trace2, trace3, trace4], layout=layout)
260+
fig.show()
287261
```
288262

289263
### Reference
290264
See https://plot.ly/python/reference/#scatter for more information and chart attribute options!
291265

292-
```python
293-
from IPython.display import display, HTML
294-
295-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
296-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
297-
298-
! pip install git+https://github.com/plotly/publisher.git --upgrade
299-
import publisher
300-
publisher.publish(
301-
'bubble.ipynb', 'python/bubble-charts/', 'Python Bubble Charts | plotly',
302-
'How to make bubble charts in Python with Plotly.',
303-
title = 'Bubble Charts | plotly',
304-
name = 'Bubble Charts', language='python',
305-
has_thumbnail='true', thumbnail='thumbnail/bubble.jpg',
306-
display_as='basic', order=3,
307-
ipynb= '~notebook_demo/1/new-to-plotly-plotlys-python-library-i',
308-
redirect_from='python/bubble-charts-tutorial/',
309-
)
310-
```
311-
312-
```python
313-
314-
```

0 commit comments

Comments
 (0)