Create Bubblechart
Tags: #plotly #chart #bubblechart #dataviz #snippet
Author: Jeremy Ravenel

Input

Import libraries

1
import naas
2
import plotly.express as px
3
import pandas as pd
Copied!

Variables

1
title = "Life Expectancy vs GDP per Capita GDP, 2007"
2
3
# Output paths
4
output_image = f"{title}.png"
5
output_html = f"{title}.html"
Copied!

Get data

1
df = px.data.gapminder()
2
df
Copied!

Model

Create Bubblechart

1
fig = px.scatter(
2
df.query("year==2007"),
3
x="gdpPercap",
4
y="lifeExp",
5
size="pop",
6
color="continent",
7
hover_name="country",
8
log_x=True,
9
size_max=60
10
)
11
12
fig.update_layout(
13
plot_bgcolor="#ffffff",
14
margin=dict(l=0, r=0, t=50, b=50),
15
width=1200,
16
height=800,
17
showlegend=False,
18
xaxis_nticks=36,
19
title= title,
20
xaxis=dict(
21
title='GDP per capita (dollars)',
22
gridcolor='white',
23
type='log',
24
gridwidth=2,
25
),
26
yaxis=dict(
27
title='Life Expectancy (years)',
28
gridcolor='white',
29
gridwidth=2,
30
))
31
32
config = {'displayModeBar': False}
33
fig.show(config=config)
Copied!

Output

Export in PNG and HTML

1
fig.write_image(output_image, width=1200)
2
fig.write_html(output_html)
Copied!

Generate shareable assets

1
link_image = naas.asset.add(output_image)
2
link_html = naas.asset.add(output_html, {"inline":True})
3
4
#-> Uncomment the line below to remove your assets
5
# naas.asset.delete(output_image)
6
# naas.asset.delete(output_html)
Copied!
Copy link
Edit on GitHub