('🇵🇹', '🇵🇹')
Flags
flag
flag (cc)
*Get the flag emoji of a country using various identifiers.
Identifiers are case-insensitive so you don’t need to worry about whether it’s in lower or upper case:
- Two-letter code: jp, mx, MX
- Three-letter code: jpn, MEX
- Country name: France, Jordan
- Official name: THE REPUBLIC OF ARMENIA, The Federal Democratic Republic of Ethiopia
- TLD (top-level domain): vn, br*
Type | Details | |
---|---|---|
cc | str | Country code, name, top-level domain or official country name |
Returns | str | The Unicode emoji flag of the respective country |
Get some country flags
Use in a Jupyter notebook
The following html
and md
functions enable HTML and markdown output to contain dynamic values.
Use in a markdown string
Use in an HTML string for more granular control
Using flags in Plotly charts
population = pd.DataFrame(
{
"country": ["China", "India", "United States", "Indonesia", "Pakistan"],
"population": [1439323776, 1380004385, 331002651, 273523615, 220892340],
"code": ["cn", "in", "us", "id", "pk"],
}
)
population
country | population | code | |
---|---|---|---|
0 | China | 1439323776 | cn |
1 | India | 1380004385 | in |
2 | United States | 331002651 | us |
3 | Indonesia | 273523615 | id |
4 | Pakistan | 220892340 | pk |
fig = px.bar(
population,
x="country",
y="population",
height=500,
text=[flag(cc) for cc in population["code"]],
template="ggplot2",
)
fig.data[0].textfont.size = 35
fig.data[0].marker.color = "steelblue"
fig
fig = px.bar(
population.sort_values("population"),
y="country",
x="population",
orientation="h",
height=500,
opacity=0.8,
text=[flag(cc) for cc in population.sort_values("population")["code"]],
template="ggplot2",
)
fig.data[0].textfont.size = 35
fig.data[0].marker.color = "teal"
fig
europe = gapminder().query("year==2007").query('continent=="Europe"')
europe["flag"] = [flag(cc) for cc in europe["iso_alpha"]]
europe.head()
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | flag | |
---|---|---|---|---|---|---|---|---|---|
23 | Albania | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 | 🇦🇱 |
83 | Austria | Europe | 2007 | 79.829 | 8199783 | 36126.492700 | AUT | 40 | 🇦🇹 |
119 | Belgium | Europe | 2007 | 79.441 | 10392226 | 33692.605080 | BEL | 56 | 🇧🇪 |
155 | Bosnia and Herzegovina | Europe | 2007 | 74.852 | 4552198 | 7446.298803 | BIH | 70 | 🇧🇦 |
191 | Bulgaria | Europe | 2007 | 73.005 | 7322858 | 10680.792820 | BGR | 100 | 🇧🇬 |
fig = px.scatter(
europe,
x="gdpPercap",
y="lifeExp",
height=600,
text="flag",
hover_data=["lifeExp", "pop", "gdpPercap"],
hover_name=europe["flag"].add(" ").add(europe["country"]),
title=f"Life expectancy ~ GDP per capita<br><b>Europe 2007</b>",
template="plotly_white",
log_y=False,
)
fig.data[0].textfont.size = 35
fig.data[0].hoverlabel.bgcolor = "white"
fig.data[0].hoverlabel.font.size = 20
fig
Cartograms improved
One misleading aspect of using maps to display data is that the area, shape, and relative position might give the wrong idea about the “size” of the metric for each country.
One way of dealing with this is creating “cartograms”, which are maps where the areas of countries are modified to reflect the size of the metric, while keeping the relative position in place.
They look very weird and even more misleading (some countries look like a tiny slice and can barely be visible due to the low value being visualized).
We can use flags to solve this issue, making diagrammatic cartograms. Flags have some interesting characteristics that can help in this:
- They have the same size, so they won’t be misleading, and they are better than country names (where their lengths can also be misleading).
- They are images, and therefore immediately recognizeable.
We will also make sure to display those flags in their respective locations, so it’s easy to find them. Now all we have to do is set text="flag"
and set the font size to a multiple of our value that would fit in our chart. This needs a bit of trial and error, because of the variance in data values.
Code
population = pd.read_html(
"https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population"
)
pop = population[0][["Location", "Population"]].copy()
flags = []
for country in pop["Location"]:
try:
flags.append(flag(country))
except Exception:
flags.append("")
continue
pop["flag"] = flags
pop = pop.dropna(subset=["flag"])
population[0][population[0]["Location"].str.contains("Czech")]
fig = px.scatter_geo(
pop,
locations="Location",
locationmode="country names",
text="flag",
size="Population",
hover_name="Location",
title="Population",
height=700,
opacity=0,
)
fig.layout.geo.showframe = False
fig.layout.geo.lataxis.range = [-53, 76]
fig.layout.geo.lonaxis.range = [-125, 171.5]
fig.layout.geo.landcolor = "white"
fig.layout.geo.showcountries = False
fig.layout.geo.coastlinecolor = "white"
fig.data[0].textfont.size = np.sqrt(pop["Population"]).div(300).add(1).round()
fig.data[0].marker.color = "white"
fig