Create Mapchart world
Tags: #plotly #chart #worldmap #dataviz #snippet
Author: Jeremy Ravenel

Input

Import libraries

1
import naas
2
import plotly.graph_objects as go
3
import pandas as pd
Copied!

Variables

1
title = "Worldmap"
2
3
# Output paths
4
output_image = f"{title}.png"
5
output_html = f"{title}.html"
Copied!

Get data

Columns :
  1. 1.
    ISO code of country
  2. 2.
    Value
To use the built-in countries geometry, provide locations as three-letter ISO country codes.
1
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
2
df
Copied!

Model

Create the plot

1
fig = go.Figure()
2
3
fig = go.Figure(data=go.Choropleth(
4
locations = df['CODE'],
5
z = df['GDP (BILLIONS)'],
6
text = df['COUNTRY'],
7
colorscale = 'Blues',
8
autocolorscale=False,
9
reversescale=True,
10
marker_line_color='darkgray',
11
marker_line_width=0.5,
12
colorbar_tickprefix = '#x27;,
13
colorbar_title = 'GDP<br>Billions US#x27;,
14
))
15
16
fig.update_layout(
17
title=title ,
18
plot_bgcolor="#ffffff",
19
legend_x=1,
20
geo=dict(
21
showframe=False,
22
showcoastlines=False,
23
#projection_type='equirectangular'
24
),
25
dragmode= False,
26
width=1200,
27
height=800,
28
29
)
30
31
config = {'displayModeBar': False}
32
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