plotly_gdp.pyΒΆ

open in new tab
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
A demo with geo data shown with plotly.
"""

from flexx import flx


records = """
Albania,13.40,ALB
Andorra,4.80,AND
Armenia,10.88,ARM
Austria,436.10,AUT
Belgium,527.80,BEL
Bosnia and Herzegovina,19.55,BIH
Bulgaria,55.08,BGR
Croatia,57.18,HRV
Cyprus,21.34,CYP
Czech Republic,205.60,CZE
Denmark,347.20,DNK
Estonia,26.36,EST
Finland,276.30,FIN
France,2902.00,FRA
United Kingdom,2848.00,GBR
Georgia,16.13,GEO
Germany,3820.00,DEU
Greece,246.40,GRC
Hungary,129.70,HUN
Ireland,245.80,IRL
Italy,2129.00,ITA
Jordan,36.55,JOR
Kosovo,5.99,KSV
Kuwait,179.30,KWT
Latvia,32.82,LVA
Luxembourg,63.93,LUX
Malta,10.57,MLT
Moldova,7.74,MDA
Monaco,6.06,MCO
Mongolia,11.73,MNG
Netherlands,880.40,NLD
Norway,511.60,NOR
Poland,552.20,POL
Portugal,228.20,PRT
Romania,199.00,ROU
Slovakia,99.75,SVK
Slovenia,49.93,SVN
Spain,1400.00,ESP
Sweden,559.10,SWE
Switzerland,679.00,CHE
Ukraine,134.90,UKR
"""

# Parse records
country_names = []
country_codes = []
country_gdps = []
for line in records.strip().splitlines():
    name, gdp, code = line.split(',')
    country_names.append(name)
    country_codes.append(code)
    country_gdps.append(float(gdp))


# Define the plot. Its probably easier to use the Python Plotly library,
# but in this way this example does not require additional dependencies.

data = [{
    'type': 'scattergeo',
    'mode': 'markers',
    'locations': country_codes,
    'marker': {
        'size': [v**0.5 for v in country_gdps],
        'color': country_gdps,
        'cmin': 0,
        'cmax': 2000,
        'colorscale': 'Viridis',
        'colorbar': {'title': 'GDP'},
        'line': {'color': 'black'}
    },
    'name': 'Europe GDP'
}]

layout = {
    'geo': {
        'scope': 'europe',
        'resolution': 50
    }
}


class PlotlyGeoDemo(flx.HBox):

    def init(self):
        flx.PlotlyWidget(data=data, layout=layout)


if __name__ == '__main__':
    flx.launch(PlotlyGeoDemo)
    flx.run()