重新定位 Colorbar plotly express / graph object Maps
Reposition Colorbar plotly express / graph object Maps
我正在尝试移动/重新定位颜色栏。我在地图上叠加了多层空间数据,颜色条相互重叠。我希望能够重新定位颜色条。
我正在使用来自以下国家/地区的美国县 GeoJSON 文件:https://eric.clst.org/tech/usgeojson/
import pandas as pd
import geopandas as gpd
import mapbox
from plotly import graph_objs as go
from plotly.graph_objs import *
# Update with host url
df_geo = gpd.read_file('https://raw.githubusercontent.com/host')
import plotly.graph_objects as go
fig = go.Figure(go.Choroplethmapbox(geojson=df_geo['geometry'].to_json(),
locations=df_geo['NAME'],
z=df_geo['CENSUSAREA'],
autocolorscale=False,
colorscale="Viridis",
zmin=df_geo['CENSUSAREA'].min(),
zmax=df_geo['CENSUSAREA'].max(),
marker_line_width=0))
fig.update_layout(mapbox_style="light",
mapbox_accesstoken=token,
mapbox_zoom=3,
mapbox_center = {"lat": 37.0902, "lon": -95.7129},
margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout()
fig.show()
- 我发现您的示例代码存在一些问题
to_json()
没有 return geojson。使用 __geo_interface__
代替
- 与 1 有关。locations 需要 link 到 id in geojson.还使用
set_index()
使其保持一致并在 geojson 中使用真实 ID
- 阿拉斯加的颜色超载,所以使用
quantile()
- 最后,问题。可以使用 colorbar 参数进行定位。
import pandas as pd
import geopandas as gpd
# import mapbox
import requests
import plotly.graph_objects as go
# Update with host url
df_geo = gpd.GeoDataFrame.from_features(
requests.get(
"https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_20m.json"
).json()
)
import plotly.graph_objects as go
fig = go.Figure(
go.Choroplethmapbox(
geojson=df_geo.set_index("GEO_ID")["geometry"].__geo_interface__,
locations=df_geo["GEO_ID"],
z=df_geo["CENSUSAREA"],
autocolorscale=False,
colorscale="Viridis",
zmin=df_geo["CENSUSAREA"].min(),
zmax=df_geo["CENSUSAREA"].quantile(0.95),
marker_line_width=0,
colorbar={"orientation": "h", "x": 0.5, "yanchor": "middle", "y": 0.1},
)
)
fig.update_layout(
mapbox_style="carto-positron",
# mapbox_accesstoken=token,
mapbox_zoom=3,
mapbox_center={"lat": 37.0902, "lon": -95.7129},
margin={"r": 0, "t": 0, "l": 0, "b": 0},
)
fig.show()
我正在尝试移动/重新定位颜色栏。我在地图上叠加了多层空间数据,颜色条相互重叠。我希望能够重新定位颜色条。
我正在使用来自以下国家/地区的美国县 GeoJSON 文件:https://eric.clst.org/tech/usgeojson/
import pandas as pd
import geopandas as gpd
import mapbox
from plotly import graph_objs as go
from plotly.graph_objs import *
# Update with host url
df_geo = gpd.read_file('https://raw.githubusercontent.com/host')
import plotly.graph_objects as go
fig = go.Figure(go.Choroplethmapbox(geojson=df_geo['geometry'].to_json(),
locations=df_geo['NAME'],
z=df_geo['CENSUSAREA'],
autocolorscale=False,
colorscale="Viridis",
zmin=df_geo['CENSUSAREA'].min(),
zmax=df_geo['CENSUSAREA'].max(),
marker_line_width=0))
fig.update_layout(mapbox_style="light",
mapbox_accesstoken=token,
mapbox_zoom=3,
mapbox_center = {"lat": 37.0902, "lon": -95.7129},
margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout()
fig.show()
- 我发现您的示例代码存在一些问题
to_json()
没有 return geojson。使用__geo_interface__
代替- 与 1 有关。locations 需要 link 到 id in geojson.还使用
set_index()
使其保持一致并在 geojson 中使用真实 ID
- 阿拉斯加的颜色超载,所以使用
quantile()
- 最后,问题。可以使用 colorbar 参数进行定位。
import pandas as pd
import geopandas as gpd
# import mapbox
import requests
import plotly.graph_objects as go
# Update with host url
df_geo = gpd.GeoDataFrame.from_features(
requests.get(
"https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_20m.json"
).json()
)
import plotly.graph_objects as go
fig = go.Figure(
go.Choroplethmapbox(
geojson=df_geo.set_index("GEO_ID")["geometry"].__geo_interface__,
locations=df_geo["GEO_ID"],
z=df_geo["CENSUSAREA"],
autocolorscale=False,
colorscale="Viridis",
zmin=df_geo["CENSUSAREA"].min(),
zmax=df_geo["CENSUSAREA"].quantile(0.95),
marker_line_width=0,
colorbar={"orientation": "h", "x": 0.5, "yanchor": "middle", "y": 0.1},
)
)
fig.update_layout(
mapbox_style="carto-positron",
# mapbox_accesstoken=token,
mapbox_zoom=3,
mapbox_center={"lat": 37.0902, "lon": -95.7129},
margin={"r": 0, "t": 0, "l": 0, "b": 0},
)
fig.show()