Links

Create Bubblechart

Tags: #plotly #chart #bubblechart #dataviz #snippet #operations #image #html
Author: Jeremy Ravenel​
Last update: 2023-04-12 (Created: 2022-03-07)
Description: This notebook provides instructions on how to create a bubble chart using Plotly.

Input

Import libraries

import naas
import plotly.express as px
import pandas as pd

Variables

title = "Life Expectancy vs GDP per Capita GDP, 2007"
​
# Output paths
output_image = f"{title}.png"
output_html = f"{title}.html"

Get data

df = px.data.gapminder()
df

Model

Create Bubblechart

fig = px.scatter(
df.query("year==2007"),
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=60,
)
​
fig.update_layout(
plot_bgcolor="#ffffff",
margin=dict(l=0, r=0, t=50, b=50),
width=1200,
height=800,
showlegend=False,
xaxis_nticks=36,
title=title,
xaxis=dict(
title="GDP per capita (dollars)",
gridcolor="white",
type="log",
gridwidth=2,
),
yaxis=dict(
title="Life Expectancy (years)",
gridcolor="white",
gridwidth=2,
),
)
​
config = {"displayModeBar": False}
fig.show(config=config)

Output

Export in PNG and HTML

fig.write_image(output_image, width=1200)
fig.write_html(output_html)

Generate shareable assets

link_image = naas.asset.add(output_image)
link_html = naas.asset.add(output_html, {"inline": True})
​
# -> Uncomment the line below to remove your assets
# naas.asset.delete(output_image)
# naas.asset.delete(output_html)