python==3.8 plotly==4.10.0
About easy scatter_geo and line_geo
You can make a figure by specifying the longitude and latitude, or by specifying the country name from iso-code. The country code is here
There are several datasets in plotly, so let's use one that already has the country name and information entered. You can check the supported data from github (https://github.com/plotly/datasets)
The country name is in iso_alpha, and I will color-code it based on the continent name.
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(df, locations="iso_alpha",
color="continent",
projection="orthographic")
fig.show()
This time I chose a globe-like format, but you can also specify a map or hemispherical display.
(Use the line_geo function to connect the lines)
The plot format must be passed to projection in character format,
'equirectangular', 'mercator', 'orthographic', 'natural earth', 'kavrayskiy7', 'miller', 'robinson', 'eckert4', 'azimuthal equal area', 'azimuthal equidistant', 'conic equal area', 'conic conformal', 'conic equidistant', 'gnomonic', 'stereographic', 'mollweide', 'hammer', 'transverse mercator', 'albers usa', 'winkel tripel', 'aitoff', 'sinusoidal'
Can be specified. Or if you want to discuss only a specific area You can also specify scope,
'world', 'usa', 'europe', 'asia', 'africa', 'north america', 'south america', 'albers usa'
You can enlarge the area of
You can also change the size of the point by passing a value to size You can also create animations by passing numerical and time data to animation_frame. It creates an animation in which the values change in the specified order.
import plotly.express as px
df = px.data.gapminder()
fig = px.scatter_geo(df,
locations="iso_alpha",
color="continent",
hover_name="country",
size="pop",
animation_frame="year",
projection="natural earth")
fig.show()
You can use FIPScode to color-code by subdivisions such as small states. To do that, you need to prepare a geojson file and pass the country code and state code, which is difficult to explain. I think it's a good idea to check as much as you need, when you need it. Since some geojson is included as a plotly dataset, it is recommended to apply it to what you want to execute while looking at the code and data.
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
dtype={"fips": str})
import plotly.express as px
fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="Viridis",
range_color=(0, 12),
scope="usa",
labels={'unemp':'unemployment rate'}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
avg_lifeExp = (df['lifeExp']*df['pop']).sum()/df['pop'].sum()
fig = px.choropleth(df, locations="iso_alpha", color="lifeExp",
color_continuous_scale=px.colors.diverging.BrBG,
color_continuous_midpoint=avg_lifeExp,
title="World Average Life Expectancy in 2007 in years was %.1f" % avg_lifeExp)
fig.show()
import plotly.express as px
df = px.data.carshare()
fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours",
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10,
mapbox_style="carto-positron")
fig.show()
Recommended Posts