diff --git a/doc/python/location-mode.md b/doc/python/location-mode.md new file mode 100644 index 00000000000..4fd607ee9d9 --- /dev/null +++ b/doc/python/location-mode.md @@ -0,0 +1,479 @@ +--- +jupyter: + jupytext: + notebook_metadata_filter: all + text_representation: + extension: .md + format_name: markdown + format_version: '1.3' + jupytext_version: 1.17.2 + kernelspec: + display_name: Python 3 (ipykernel) + language: python + name: python3 + language_info: + codemirror_mode: + name: ipython + version: 3 + file_extension: .py + mimetype: text/x-python + name: python + nbconvert_exporter: python + pygments_lexer: ipython3 + version: 3.10.0 + plotly: + description: How to specify country codes, names, and US states for outline-based + maps + display_as: maps + language: python + layout: base + name: Locations for Outline-based Maps + order: 15 + page_type: example_index + permalink: python/outline-map-locations/ + thumbnail: thumbnail/choropleth.jpg +--- + +# Locations for Outline-based Maps + +With outline-based maps, you can visualize data for specific regions using the `locations` and `locationmode` parameters. + +The following map types in `plotly.express` and `plotly.graph_objects` support these parameters: + +- `px.choropleth` - color regions based on data values +- `px.scatter_geo` - show markers at geographic locations +- `px.line_geo` - draw lines connecting geographic locations +- `go.Choropleth` - choropleth trace for coloring regions +- `go.Scattergeo` - geographic scatter/line trace for markers and lines + +The `locations` parameter accepts region identifiers and the `locationmode` parameter controls how those identifiers are interpreted: + +- `'ISO-3'` - three-letter ISO country codes (for example, `'USA'`, `'CAN'`, `'GBR'`) +- `'USA-states'` - two-letter US state abbreviations (for example, `'CA'`, `'TX'`, `'NY'`) +- `'country names'` - full country names (for example, `'United States'`) + + +## locationmode='ISO-3' + +Set `locationmode='ISO-3'` to use three-letter ISO country codes in `locations`. + +```python +import plotly.express as px + +fig = px.choropleth( + locations=['USA', 'CAN', 'MEX', 'BRA', 'RUS'], + locationmode='ISO-3', + color=[100, 85, 72, 95, 68], + color_continuous_scale='Viridis', + title='Choropleth with ISO-3 Country Codes' +) +fig.show() +``` + +## Supported ISO Codes + +The following ISO codes are supported when `locationmode='ISO-3'`: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameISO Code
AfghanistanAFG
Aksai ChinXAC
Åland IslandsALA
AlbaniaALB
AlgeriaDZA
American SamoaASM
AndorraAND
AngolaAGO
AnguillaAIA
AntarcticaATA
Antigua and BarbudaATG
ArgentinaARG
ArmeniaARM
ArubaABW
Arunachal PradeshXAP
AustraliaAUS
AustriaAUT
AzerbaijanAZE
Azores IslandsPRT
BahamasBHS
BahrainBHR
BangladeshBGD
BarbadosBRB
BelarusBLR
BelgiumBEL
BelizeBLZ
BeninBEN
BermudaBMU
BhutanBTN
Bir TawilXBT
Bolivia (Plurinational State of)BOL
BonaireBES
Bosnia and HerzegovinaBIH
BotswanaBWA
Bouvet IslandBVT
BrazilBRA
British Virgin IslandsVGB
Brunei DarussalamBRN
BulgariaBGR
Burkina FasoBFA
BurundiBDI
Cabo VerdeCPV
CambodiaKHM
CameroonCMR
CanadaCAN
Canary IslandsESP
Cayman IslandsCYM
Central African RepublicCAF
ChadTCD
Chagos ArchipelagoMUS
ChileCHL
ChinaCHN
Christmas IslandCXR
Cocos (Keeling) IslandsCCK
ColombiaCOL
ComorosCOM
CongoCOG
Cook IslandsCOK
Costa RicaCRI
Côte d'IvoireCIV
CroatiaHRV
CubaCUB
CuraçaoCUW
CyprusCYP
CzechiaCZE
Democratic People's Republic of KoreaPRK
Democratic Republic of the CongoCOD
DenmarkDNK
DjiboutiDJI
DominicaDMA
Dominican RepublicDOM
EcuadorECU
EgyptEGY
El SalvadorSLV
Equatorial GuineaGNQ
EritreaERI
EstoniaEST
EswatiniSWZ
EthiopiaETH
Falkland Islands (Malvinas)FLK
Faroe IslandsFRO
FijiFJI
FinlandFIN
FranceFRA
French GuianaGUF
French PolynesiaPYF
French Southern TerritoriesATF
GabonGAB
Galápagos IslandsECU
GambiaGMB
GazaPSE
GeorgiaGEO
GermanyDEU
GhanaGHA
GibraltarGIB
GreeceGRC
GreenlandGRL
GrenadaGRD
GuadeloupeGLP
GuamGUM
GuatemalaGTM
GuernseyGGY
GuineaGIN
Guinea-BissauGNB
GuyanaGUY
HaitiHTI
Halaib TriangleXHT
Heard Island and McDonald IslandsHMD
HondurasHND
Hong KongHKG
HungaryHUN
IcelandISL
Ilemi TriangleXIT
IndiaIND
IndonesiaIDN
Iran (Islamic Republic of)IRN
IraqIRQ
IrelandIRL
Isle of ManIMN
IsraelISR
ItalyITA
JamaicaJAM
Jammu and KashmirXJK
JapanJPN
JerseyJEY
JordanJOR
KazakhstanKAZ
KenyaKEN
Kingdom of the NetherlandsNLD
KiribatiKIR
KuwaitKWT
KyrgyzstanKGZ
Lao People's Democratic RepublicLAO
LatviaLVA
LebanonLBN
LesothoLSO
LiberiaLBR
LibyaLBY
LiechtensteinLIE
LithuaniaLTU
LuxembourgLUX
MacaoMAC
MadagascarMDG
Madeira IslandPRT
MalawiMWI
MalaysiaMYS
MaldivesMDV
MaliMLI
MaltaMLT
Marshall IslandsMHL
MartiniqueMTQ
MauritaniaMRT
MauritiusMUS
MayotteMYT
MexicoMEX
Micronesia (Federated States of)FSM
MonacoMCO
MongoliaMNG
MontenegroMNE
MontserratMSR
MoroccoMAR
MozambiqueMOZ
MyanmarMMR
NamibiaNAM
NauruNRU
NepalNPL
New CaledoniaNCL
New ZealandNZL
NicaraguaNIC
NigerNER
NigeriaNGA
NiueNIU
Norfolk IslandNFK
North MacedoniaMKD
Northern Mariana IslandsMNP
NorwayNOR
OmanOMN
PakistanPAK
PalauPLW
PanamaPAN
Papua New GuineaPNG
ParaguayPRY
PeruPER
PhilippinesPHL
PitcairnPCN
PolandPOL
PortugalPRT
Puerto RicoPRI
QatarQAT
Republic of KoreaKOR
Republic of MoldovaMDA
RéunionREU
RomaniaROU
Russian FederationRUS
RwandaRWA
SabaBES
Saint BarthélemyBLM
Saint HelenaSHN
Saint Kitts and NevisKNA
Saint LuciaLCA
Saint MartinMAF
Saint Pierre and MiquelonSPM
Saint Vincent and the GrenadinesVCT
SamoaWSM
Sao Tome and PrincipeSTP
Saudi ArabiaSAU
SenegalSEN
SerbiaSRB
SeychellesSYC
Sierra LeoneSLE
SingaporeSGP
Sint EustatiusBES
Sint MaartenSXM
SlovakiaSVK
SloveniaSVN
Solomon IslandsSLB
SomaliaSOM
South AfricaZAF
South Georgia and the South Sandwich IslandsSGS
South SudanSSD
SpainESP
Sri LankaLKA
SudanSDN
SurinameSUR
Svalbard and Jan Mayen IslandsSJM
SwedenSWE
SwitzerlandCHE
Syrian Arab RepublicSYR
TaiwanTWN
TajikistanTJK
ThailandTHA
Timor-LesteTLS
TogoTGO
TokelauTKL
TongaTON
Trinidad and TobagoTTO
TunisiaTUN
TürkiyeTUR
TurkmenistanTKM
Turks and Caicos IslandsTCA
TuvaluTUV
UgandaUGA
UkraineUKR
United Arab EmiratesARE
United Kingdom of Great Britain and Northern IrelandGBR
United Republic of TanzaniaTZA
United States of AmericaUSA
United States Virgin IslandsVIR
UruguayURY
UzbekistanUZB
VanuatuVUT
Venezuela (Bolivarian Republic of)VEN
Viet NamVNM
West BankPSE
Western SaharaESH
YemenYEM
ZambiaZMB
ZimbabweZWE
+ + +## locationmode='USA-states' + +Set `locationmode='USA-states'` to use two-letter US state abbreviations in `locations`. + +```python +import plotly.express as px + +fig = px.choropleth( + locations=['CA', 'TX', 'NY', 'FL', 'IL'], + locationmode='USA-states', + color=[95, 88, 92, 85, 78], + scope='usa', + color_continuous_scale='Reds', + title='USA States Choropleth' +) +fig.show() +``` + +## Supported US State Codes + +The following state codes are supported when `locationmode='USA-states'`: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
StateCode
AlabamaAL
AlaskaAK
ArizonaAZ
ArkansasAR
CaliforniaCA
ColoradoCO
ConnecticutCT
DelawareDE
District of ColumbiaDC
FloridaFL
GeorgiaGA
HawaiiHI
IdahoID
IllinoisIL
IndianaIN
IowaIA
KansasKS
KentuckyKY
LouisianaLA
MaineME
MarylandMD
MassachusettsMA
MichiganMI
MinnesotaMN
MississippiMS
MissouriMO
MontanaMT
NebraskaNE
NevadaNV
New HampshireNH
New JerseyNJ
New MexicoNM
New YorkNY
North CarolinaNC
North DakotaND
OhioOH
OklahomaOK
OregonOR
PennsylvaniaPA
Rhode IslandRI
South CarolinaSC
South DakotaSD
TennesseeTN
TexasTX
UtahUT
VermontVT
VirginiaVA
WashingtonWA
West VirginiaWV
WisconsinWI
WyomingWY
+ + +## locationmode='country names' + +Set `locationmode='country names'` to use full country names in `locations`. + +```python +import plotly.express as px + +fig = px.choropleth( + locations=['United States', 'Canada', 'United Kingdom'], + locationmode='country names' +) +fig.show() +``` + +> How Plotly matches 'country names' will change in a future version. Matching will become stricter and some country names may no longer match. We recommend using `locationmode='ISO-3'` with ISO codes for `locations` to ensure consistent behavior across versions. + +```python +import plotly.express as px + +fig = px.choropleth( + locations=['USA', 'CAN', 'GBR'], + locationmode='ISO-3' +) +fig.show() +``` + +## Using Different Data Types with `locations` + +Earlier examples demonstrated using the `locations` parameter with Python lists. The `locations` parameter also accepts column names from DataFrames, pandas Series, or other array-like objects. + +Here's an example that uses a column from the gapminder dataset with `locations`: + +```python +import plotly.express as px + +df = px.data.gapminder().query("year == 2007") + +fig = px.choropleth( + df, + locations='iso_alpha', + locationmode='ISO-3', + color='lifeExp', + hover_name='country', + color_continuous_scale='Viridis', + title='Life Expectancy by Country (2007)' +) +fig.show() +```