Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3769dac

Browse files
authored
Merge pull request#11 from plotly/candlestick-andothers
candlestick; table; network-graphs; box-plots; gantt ; sankey-diagram; filled-area-plots; distplot; tree-plots; bubble-charts
2 parentsa92a30d +bad179b commit3769dac

File tree

10 files changed

+857
-1421
lines changed

10 files changed

+857
-1421
lines changed

‎notebooks/box-plots.md

Lines changed: 239 additions & 262 deletions
Large diffs are not rendered by default.

‎notebooks/bubble-charts.md

Lines changed: 94 additions & 180 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:Python2
11+
display_name:Python3
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
@@ -24,59 +35,60 @@ jupyter:
2435
redirect_from:python/bubble-charts-tutorial/
2536
thumbnail:thumbnail/bubble.jpg
2637
title:Bubble Charts | plotly
38+
v4upgrade:true
2739
---
2840

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.
41+
##Bubble chart with plotly.express
42+
43+
A[bubble chart](https://en.wikipedia.org/wiki/Bubble_chart) is a scatter plot in which a third dimension of the data is shown through the size of markers. For other types of scatter plot, see the[line and scatter page](https://plot.ly/python/line-and-scatter/).
44+
45+
We first show a bubble chart example using plotly express. Plotly express functions take as argument a tidy[pandas DataFrame](https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html). The size of markers is set from the dataframe column given as the`size` parameter.
3546

3647
```python
37-
import plotly
38-
plotly.__version__
48+
import plotly.expressas px
49+
gapminder= px.data.gapminder()
50+
51+
fig= px.scatter(gapminder.query("year==2007"),x="gdpPercap",y="lifeExp",
52+
size="pop",color="continent",
53+
hover_name="country",log_x=True,size_max=60)
54+
fig.show()
3955
```
4056

57+
##Bubble Chart with plotly.graph_objects
58+
59+
When data are not available as tidy dataframes, it is also possible to use the more generic`go.Scatter` from`plotly.graph_objects`, and define the size of markers to create a bubble chart. All of the available options are described in the scatter section of the reference page:https://plot.ly/python/reference#scatter.
60+
4161
###Simple Bubble Chart
4262

4363
```python
44-
import plotly.plotlyas py
45-
import plotly.graph_objsas go
64+
import plotly.graph_objectsas go
4665

47-
trace0= go.Scatter(
48-
x=[1,2,3,4],
49-
y=[10,11,12,13],
66+
fig= go.Figure(data=[go.Scatter(
67+
x=[1,2,3,4],y=[10,11,12,13],
5068
mode='markers',
51-
marker=dict(
52-
size=[40,60,80,100],
53-
)
54-
)
55-
56-
data= [trace0]
57-
py.iplot(data,filename='bubblechart-size')
69+
marker_size=[40,60,80,100])
70+
])
71+
72+
fig.show()
5873
```
5974

6075
###Setting Marker Size and Color
6176

6277
```python
63-
import plotly.plotlyas py
64-
import plotly.graph_objsas go
78+
import plotly.graph_objectsas go
6579

66-
trace0= go.Scatter(
67-
x=[1,2,3,4],
68-
y=[10,11,12,13],
80+
fig= go.Figure(data=[go.Scatter(
81+
x=[1,2,3,4],y=[10,11,12,13],
6982
mode='markers',
7083
marker=dict(
7184
color=['rgb(93, 164, 214)','rgb(255, 144, 14)',
7285
'rgb(44, 160, 101)','rgb(255, 65, 54)'],
7386
opacity=[1,0.8,0.6,0.4],
7487
size=[40,60,80,100],
7588
)
76-
)
89+
)])
7790

78-
data= [trace0]
79-
py.iplot(data,filename='bubblechart-color')
91+
fig.show()
8092
```
8193

8294
###Scaling the Size of Bubble Charts
@@ -86,11 +98,10 @@ Note that setting 'sizeref' to a value greater than 1, decreases the rendered ma
8698
Additionally, we recommend setting the sizemode attribute:https://plot.ly/python/reference/#scatter-marker-sizemode to area.
8799

88100
```python
89-
import plotly.plotlyas py
90-
import plotly.graph_objsas go
101+
import plotly.graph_objectsas go
91102

92103
size= [20,40,60,80,100,80,60,40,20,40]
93-
trace0= go.Scatter(
104+
fig=go.Figure(data=[go.Scatter(
94105
x=[1,2,3,4,5,6,7,8,9,10],
95106
y=[11,12,10,11,12,11,12,13,12,11],
96107
mode='markers',
@@ -100,68 +111,61 @@ trace0 = go.Scatter(
100111
sizeref=2.*max(size)/(40.**2),
101112
sizemin=4
102113
)
103-
)
114+
)])
104115

105-
data= [trace0]
106-
py.iplot(data,filename='bubblechart-size-ref')
116+
fig.show()
107117
```
108118

109119
###Hover Text with Bubble Charts
110120

111121
```python
112-
import plotly.plotlyas py
113-
import plotly.graph_objsas go
122+
import plotly.graph_objectsas go
114123

115-
trace0= go.Scatter(
116-
x=[1,2,3,4],
117-
y=[10,11,12,13],
124+
fig= go.Figure(data=[go.Scatter(
125+
x=[1,2,3,4],y=[10,11,12,13],
118126
text=['A<br>size: 40','B<br>size: 60','C<br>size: 80','D<br>size: 100'],
119127
mode='markers',
120128
marker=dict(
121129
color=['rgb(93, 164, 214)','rgb(255, 144, 14)','rgb(44, 160, 101)','rgb(255, 65, 54)'],
122130
size=[40,60,80,100],
123131
)
124-
)
132+
)])
125133

126-
data= [trace0]
127-
py.iplot(data,filename='bubblechart-text')
134+
fig.show()
128135
```
129136

130137
###Bubble Charts with Colorscale
131138

132139
```python
133-
import plotly.plotlyas py
134-
import plotly.graph_objsas go
135-
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')
140+
import plotly.graph_objectsas go
141+
142+
fig= go.Figure(data=[go.Scatter(
143+
x=[1,3.2,5.4,7.6,9.8,12.5],
144+
y=[1,3.2,5.4,7.6,9.8,12.5],
145+
mode='markers',
146+
marker=dict(
147+
color=[120,125,130,135,140,145],
148+
size=[15,30,55,70,90,110],
149+
showscale=True
150+
)
151+
)])
152+
153+
fig.show()
150154
```
151155

152156
###Categorical Bubble Charts
153157

154158
```python
155-
import plotly.plotlyas py
156-
import plotly.graph_objsas go
157-
159+
import plotly.graph_objectsas go
160+
import plotly.expressas px
158161
import pandasas pd
159162
import math
160163

161-
data= pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
164+
# Load data, define hover text and bubble size
165+
data= px.data.gapminder()
162166
df_2007= data[data['year']==2007]
163167
df_2007= df_2007.sort_values(['continent','country'])
164-
slope=2.666051223553066e-05
168+
165169
hover_text= []
166170
bubble_size= []
167171

@@ -175,140 +179,50 @@ for index, row in df_2007.iterrows():
175179
gdp=row['gdpPercap'],
176180
pop=row['pop'],
177181
year=row['year']))
178-
bubble_size.append(math.sqrt(row['pop']*slope))
182+
bubble_size.append(math.sqrt(row['pop']))
179183

180184
df_2007['text']= hover_text
181185
df_2007['size']= bubble_size
182186
sizeref=2.*max(df_2007['size'])/(100**2)
183187

184-
trace0= go.Scatter(
185-
x=df_2007['gdpPercap'][df_2007['continent']=='Africa'],
186-
y=df_2007['lifeExp'][df_2007['continent']=='Africa'],
187-
mode='markers',
188-
name='Africa',
189-
text=df_2007['text'][df_2007['continent']=='Africa'],
190-
marker=dict(
191-
symbol='circle',
192-
sizemode='area',
193-
sizeref=sizeref,
194-
size=df_2007['size'][df_2007['continent']=='Africa'],
195-
line=dict(
196-
width=2
197-
),
198-
)
199-
)
200-
trace1= go.Scatter(
201-
x=df_2007['gdpPercap'][df_2007['continent']=='Americas'],
202-
y=df_2007['lifeExp'][df_2007['continent']=='Americas'],
203-
mode='markers',
204-
name='Americas',
205-
text=df_2007['text'][df_2007['continent']=='Americas'],
206-
marker=dict(
207-
sizemode='area',
208-
sizeref=sizeref,
209-
size=df_2007['size'][df_2007['continent']=='Americas'],
210-
line=dict(
211-
width=2
212-
),
213-
)
214-
)
215-
trace2= go.Scatter(
216-
x=df_2007['gdpPercap'][df_2007['continent']=='Asia'],
217-
y=df_2007['lifeExp'][df_2007['continent']=='Asia'],
218-
mode='markers',
219-
name='Asia',
220-
text=df_2007['text'][df_2007['continent']=='Asia'],
221-
marker=dict(
222-
sizemode='area',
223-
sizeref=sizeref,
224-
size=df_2007['size'][df_2007['continent']=='Asia'],
225-
line=dict(
226-
width=2
227-
),
228-
)
229-
)
230-
trace3= go.Scatter(
231-
x=df_2007['gdpPercap'][df_2007['continent']=='Europe'],
232-
y=df_2007['lifeExp'][df_2007['continent']=='Europe'],
233-
mode='markers',
234-
name='Europe',
235-
text=df_2007['text'][df_2007['continent']=='Europe'],
236-
marker=dict(
237-
sizemode='area',
238-
sizeref=sizeref,
239-
size=df_2007['size'][df_2007['continent']=='Europe'],
240-
line=dict(
241-
width=2
242-
),
243-
)
244-
)
245-
trace4= go.Scatter(
246-
x=df_2007['gdpPercap'][df_2007['continent']=='Oceania'],
247-
y=df_2007['lifeExp'][df_2007['continent']=='Oceania'],
248-
mode='markers',
249-
name='Oceania',
250-
text=df_2007['text'][df_2007['continent']=='Oceania'],
251-
marker=dict(
252-
sizemode='area',
253-
sizeref=sizeref,
254-
size=df_2007['size'][df_2007['continent']=='Oceania'],
255-
line=dict(
256-
width=2
257-
),
258-
)
259-
)
260-
261-
data= [trace0, trace1, trace2, trace3, trace4]
262-
layout= go.Layout(
188+
# Dictionary with dataframes for each continent
189+
continent_names= ['Africa','Americas','Asia','Europe','Oceania']
190+
continent_data= {continent:df_2007.query("continent == '%s'"%continent)
191+
for continentin continent_names}
192+
193+
# Create figure
194+
fig= go.Figure()
195+
196+
for continent_name, continentin continent_data.items():
197+
fig.add_trace(go.Scatter(
198+
x=continent['gdpPercap'],y=continent['lifeExp'],
199+
name=continent_name,text=continent['text'],
200+
marker_size=continent['size'],
201+
))
202+
203+
# Tune marker appearance and layout
204+
fig.update_traces(mode='markers',marker=dict(sizemode='area',
205+
sizeref=sizeref,line_width=2))
206+
207+
fig.update_layout(
263208
title='Life Expectancy v. Per Capita GDP, 2007',
264209
xaxis=dict(
265210
title='GDP per capita (2000 dollars)',
266-
gridcolor='rgb(255, 255, 255)',
267-
range=[2.003297660701705,5.191505530708712],
211+
gridcolor='white',
268212
type='log',
269-
zerolinewidth=1,
270-
ticklen=5,
271213
gridwidth=2,
272214
),
273215
yaxis=dict(
274216
title='Life Expectancy (years)',
275-
gridcolor='rgb(255, 255, 255)',
276-
range=[36.12621671352166,91.72921793264332],
277-
zerolinewidth=1,
278-
ticklen=5,
217+
gridcolor='white',
279218
gridwidth=2,
280219
),
281220
paper_bgcolor='rgb(243, 243, 243)',
282221
plot_bgcolor='rgb(243, 243, 243)',
283222
)
284-
285-
fig= go.Figure(data=data,layout=layout)
286-
py.iplot(fig,filename='life-expectancy-per-GDP-2007')
223+
fig.show()
287224
```
288225

289226
###Reference
290227
Seehttps://plot.ly/python/reference/#scatter for more information and chart attribute options!
291228

292-
```python
293-
from IPython.displayimport 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)

[8]ページ先頭

©2009-2025 Movatter.jp