- Notifications
You must be signed in to change notification settings - Fork1
[MRG] Candlestick andothers#11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
27ecd50
75fca75
7c9edb6
2c8e46d
991a6b2
295631e
4d68915
7d0bd9f
37284c3
1260c02
4082a8c
e778e0b
bad179b
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
--- | ||
jupyter: | ||
jupytext: | ||
notebook_metadata_filter: all | ||
text_representation: | ||
extension: .md | ||
format_name: markdown | ||
format_version: '1.1' | ||
jupytext_version: 1.1.1 | ||
kernelspec: | ||
display_name: Python3 | ||
language: python | ||
name: python3 | ||
language_info: | ||
codemirror_mode: | ||
name: ipython | ||
version: 3 | ||
file_extension: .py | ||
mimetype: text/x-python | ||
name: python | ||
nbconvert_exporter: python | ||
pygments_lexer: ipython3 | ||
version: 3.6.7 | ||
plotly: | ||
description: How to make bubble charts in Python with Plotly. | ||
display_as: basic | ||
@@ -24,59 +35,60 @@ jupyter: | ||
redirect_from: python/bubble-charts-tutorial/ | ||
thumbnail: thumbnail/bubble.jpg | ||
title: Bubble Charts | plotly | ||
v4upgrade: true | ||
--- | ||
## Bubble chart with plotly.express | ||
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/). | ||
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. | ||
```python | ||
import plotly.express as px | ||
gapminder = px.data.gapminder() | ||
fig = px.scatter(gapminder.query("year==2007"), x="gdpPercap", y="lifeExp", | ||
size="pop", color="continent", | ||
hover_name="country", log_x=True, size_max=60) | ||
fig.show() | ||
``` | ||
## Bubble Chart with plotly.graph_objects | ||
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. | ||
### Simple Bubble Chart | ||
emmanuelle marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
```python | ||
import plotly.graph_objects as go | ||
fig = go.Figure(data=[go.Scatter( | ||
x=[1, 2, 3, 4], y=[10, 11, 12, 13], | ||
mode='markers', | ||
marker_size=[40, 60, 80, 100]) | ||
]) | ||
fig.show() | ||
``` | ||
### Setting Marker Size and Color | ||
```python | ||
import plotly.graph_objects as go | ||
fig = go.Figure(data=[go.Scatter( | ||
x=[1, 2, 3, 4], y=[10, 11, 12, 13], | ||
mode='markers', | ||
marker=dict( | ||
color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)', | ||
'rgb(44, 160, 101)', 'rgb(255, 65, 54)'], | ||
opacity=[1, 0.8, 0.6, 0.4], | ||
size=[40, 60, 80, 100], | ||
) | ||
)]) | ||
fig.show() | ||
``` | ||
### Scaling the Size of Bubble Charts | ||
@@ -86,11 +98,10 @@ Note that setting 'sizeref' to a value greater than 1, decreases the rendered ma | ||
Additionally, we recommend setting the sizemode attribute: https://plot.ly/python/reference/#scatter-marker-sizemode to area. | ||
```python | ||
import plotly.graph_objects as go | ||
size = [20, 40, 60, 80, 100, 80, 60, 40, 20, 40] | ||
fig =go.Figure(data=[go.Scatter( | ||
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | ||
y=[11, 12, 10, 11, 12, 11, 12, 13, 12, 11], | ||
mode='markers', | ||
@@ -100,68 +111,61 @@ trace0 = go.Scatter( | ||
sizeref=2.*max(size)/(40.**2), | ||
sizemin=4 | ||
) | ||
)]) | ||
fig.show() | ||
``` | ||
### Hover Text with Bubble Charts | ||
```python | ||
import plotly.graph_objects as go | ||
fig = go.Figure(data=[go.Scatter( | ||
x=[1, 2, 3, 4], y=[10, 11, 12, 13], | ||
text=['A<br>size: 40', 'B<br>size: 60', 'C<br>size: 80', 'D<br>size: 100'], | ||
mode='markers', | ||
marker=dict( | ||
color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)', 'rgb(44, 160, 101)', 'rgb(255, 65, 54)'], | ||
size=[40, 60, 80, 100], | ||
) | ||
)]) | ||
fig.show() | ||
``` | ||
### Bubble Charts with Colorscale | ||
```python | ||
import plotly.graph_objects as go | ||
fig = go.Figure(data=[go.Scatter( | ||
x=[1, 3.2, 5.4, 7.6, 9.8, 12.5], | ||
y=[1, 3.2, 5.4, 7.6, 9.8, 12.5], | ||
mode='markers', | ||
marker=dict( | ||
color=[120, 125, 130, 135, 140, 145], | ||
size=[15, 30, 55, 70, 90, 110], | ||
showscale=True | ||
) | ||
)]) | ||
fig.show() | ||
``` | ||
### Categorical Bubble Charts | ||
```python | ||
import plotly.graph_objects as go | ||
import plotly.express as px | ||
import pandas as pd | ||
import math | ||
# Load data, define hover text and bubble size | ||
data = px.data.gapminder() | ||
df_2007 = data[data['year']==2007] | ||
df_2007 = df_2007.sort_values(['continent', 'country']) | ||
hover_text = [] | ||
bubble_size = [] | ||
@@ -175,140 +179,50 @@ for index, row in df_2007.iterrows(): | ||
gdp=row['gdpPercap'], | ||
pop=row['pop'], | ||
year=row['year'])) | ||
bubble_size.append(math.sqrt(row['pop'])) | ||
df_2007['text'] = hover_text | ||
df_2007['size'] = bubble_size | ||
sizeref = 2.*max(df_2007['size'])/(100**2) | ||
# Dictionary with dataframes for each continent | ||
continent_names = ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania'] | ||
continent_data = {continent:df_2007.query("continent == '%s'" %continent) | ||
for continent in continent_names} | ||
# Create figure | ||
fig = go.Figure() | ||
for continent_name, continent in continent_data.items(): | ||
emmanuelle marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
fig.add_trace(go.Scatter( | ||
x=continent['gdpPercap'], y=continent['lifeExp'], | ||
name=continent_name, text=continent['text'], | ||
marker_size=continent['size'], | ||
)) | ||
# Tune marker appearance and layout | ||
fig.update_traces(mode='markers', marker=dict(sizemode='area', | ||
sizeref=sizeref, line_width=2)) | ||
fig.update_layout( | ||
title='Life Expectancy v. Per Capita GDP, 2007', | ||
xaxis=dict( | ||
title='GDP per capita (2000 dollars)', | ||
gridcolor='white', | ||
type='log', | ||
gridwidth=2, | ||
), | ||
yaxis=dict( | ||
title='Life Expectancy (years)', | ||
gridcolor='white', | ||
gridwidth=2, | ||
), | ||
paper_bgcolor='rgb(243, 243, 243)', | ||
plot_bgcolor='rgb(243, 243, 243)', | ||
) | ||
fig.show() | ||
``` | ||
### Reference | ||
See https://plot.ly/python/reference/#scatter for more information and chart attribute options! | ||
Uh oh!
There was an error while loading.Please reload this page.