import geopandas as gpd
import pandas as pd
import plotly.express as px
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(mapbox_style="stamen-terrain")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig = go.Figure(go.Scattermapbox(
mode = "markers+lines",
lon = [10, 20, 30],
lat = [10, 20,30],
marker = {'size': 10}))
fig.add_trace(go.Scattermapbox(
mode = "markers+lines",
lon = [-50, -60,40],
lat = [30, 10, -20],
marker = {'size': 10}))
fig.update_layout(
margin ={'l':0,'t':0,'b':0,'r':0},
mapbox = {
'center': {'lon': 10, 'lat': 10},
'style': "stamen-terrain",
'center': {'lon': -20, 'lat': -20},
'zoom': 1})
fig.show()
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(
mapbox_style="white-bg",
mapbox_layers=[
{
"below": 'traces',
"sourcetype": "raster",
"sourceattribution": "United States Geological Survey",
"source": [
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
]
}
])
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(
mapbox_style="white-bg",
mapbox_layers=[
{
"below": 'traces',
"sourcetype": "raster",
"sourceattribution": "United States Geological Survey",
"source": [
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
]
},
{
"sourcetype": "raster",
"sourceattribution": "Government of Canada",
"source": ["https://geo.weather.gc.ca/geomet/?"
"SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX={bbox-epsg-3857}&CRS=EPSG:3857"
"&WIDTH=1000&HEIGHT=1000&LAYERS=RADAR_1KM_RDBR&TILED=true&FORMAT=image/png"],
}
])
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(mapbox_style="satellite-streets", mapbox_accesstoken='pk.eyJ1IjoibGluY2hlbnZhIiwiYSI6ImNrbHI0cnZzaTBha2wydm1pdHVoczFrdmYifQ.R40_D1qPC-tllkKrtACAow')
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
px.set_mapbox_access_token('pk.eyJ1IjoibGluY2hlbnZhIiwiYSI6ImNrbHI0cnZzaTBha2wydm1pdHVoczFrdmYifQ.R40_D1qPC-tllkKrtACAow')
df = px.data.carshare()
# by default, use light style
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)
fig.show()
import plotly.graph_objects as go
fig = go.Figure(go.Scattermapbox(
mode = "markers+text+lines",
lon = [-75, -80, -50], lat = [45, 20, -20],
marker = {'size': 20, 'symbol': ["bicycle", "harbor", "airport"]},
text = ["Bus", "Harbor", "airport"],textposition = "bottom right"))
# "basic", "streets", "outdoors", "light", "dark", "satellite", or "satellite-streets"
fig.update_layout(
mapbox = {
'accesstoken': 'pk.eyJ1IjoibGluY2hlbnZhIiwiYSI6ImNrbHI0cnZzaTBha2wydm1pdHVoczFrdmYifQ.R40_D1qPC-tllkKrtACAow',
'style': "satellite", 'zoom': 0.7},
showlegend = False)
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})
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="Viridis",
range_color=(0, 12),
mapbox_style="stamen-terrain",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.2,
labels={'unemp':'unemployment rate'}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
df = px.data.election()
geojson = px.data.election_geojson()
fig = px.choropleth_mapbox(df, geojson=geojson, color="Bergeron",
locations="district", featureidkey="properties.district",
center={"lat": 45.5517, "lon": -73.7073},
mapbox_style="carto-positron", zoom=9)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
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.graph_objects as go
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=df.fips, z=df.unemp,
colorscale="Viridis", zmin=0, zmax=12,
marker_opacity=0.5, marker_line_width=0))
fig.update_layout(mapbox_style="carto-positron",
mapbox_zoom=3, mapbox_center = {"lat": 37.0902, "lon": -95.7129})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
us_cities = us_cities.query("State in ['New York', 'Ohio']")
us_cities.head()
fig = px.line_mapbox(us_cities, lat="lat", lon="lon", color="State", zoom=3, height=300)
fig.update_layout(mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_lat = 41,
margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
import plotly.express as px
fig = px.line_mapbox(lat=[0,15,None, 20,35], lon=[5,10,None, 25,30], hover_name = ['t0<br>temp', 't1', None, 't2', 't3'])
fig.update_mapboxes(center_lon=20, center_lat=20)
fig.update_layout(mapbox_style="stamen-terrain", mapbox_zoom=3,
margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
import shapely.geometry
import numpy as np
import wget
wget.download("https://plotly.github.io/datasets/ne_50m_rivers_lake_centerlines.zip")
# open a zipped shapefile with the zip:// pseudo-protocol
geo_df = gpd.read_file("zip://ne_50m_rivers_lake_centerlines.zip")
lats = []
lons = []
names = []
for feature, name in zip(geo_df.geometry, geo_df.name):
if isinstance(feature, shapely.geometry.linestring.LineString):
linestrings = [feature]
elif isinstance(feature, shapely.geometry.multilinestring.MultiLineString):
linestrings = feature.geoms
else:
continue
for linestring in linestrings:
x, y = linestring.xy
lats = np.append(lats, y)
lons = np.append(lons, x)
names = np.append(names, [name]*len(y))
lats = np.append(lats, None)
lons = np.append(lons, None)
names = np.append(names, None)
fig = px.line_mapbox(lat=lats, lon=lons, hover_name=names,
mapbox_style="stamen-terrain", zoom=1)
fig.show()
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')
fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
center=dict(lat=0, lon=180), zoom=0,
mapbox_style="stamen-terrain")
fig.show()
quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')
fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude,
radius=10))
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
import geopandas as gpd
# convert geopandas Dataframe from geojson
gdf = gpd.GeoDataFrame.from_features(counties['features'])
# convert geopandas to geojson
gjson = gdf[['geometry', 'GEO_ID']].to_json() # str
gjson = json.loads(gjson) # json
df = px.data.election()
geo_df = gpd.GeoDataFrame.from_features(px.data.election_geojson()["features"]).merge(df, on="district")
fig = px.choropleth_mapbox(geo_df,
geojson=geo_df.geometry,
locations=geo_df.index,
color="Joly",
center={"lat": 45.5517, "lon": -73.7073},
mapbox_style="open-street-map",
zoom=8.5)
fig.show()
geo_df = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
px.set_mapbox_access_token('pk.eyJ1IjoibGluY2hlbnZhIiwiYSI6ImNrbHI0cnZzaTBha2wydm1pdHVoczFrdmYifQ.R40_D1qPC-tllkKrtACAow')
fig = px.scatter_mapbox(geo_df,
lat=geo_df.geometry.y,
lon=geo_df.geometry.x,
hover_name="name",
zoom=1)
fig.show()
fig = go.Figure(go.Scattermapbox(
fill = "toself",
lon = [-74, -70, -70, -74], lat = [47, 47, 45, 45],
marker = { 'size': 10, 'color': "orange" }))
fig.update_layout(
mapbox = {
'style': "stamen-terrain",
'center': {'lon': -73, 'lat': 46 },
'zoom': 5},
showlegend = False)
fig.show()