Plotly 等值线图不同方面的不同色标?
Different color scales for different facets of choropleth map in Plotly?
我制作了一个具有连续色标的等值线图并将其划分为 4 个子图面。问题是色标尺是所有 4 张地图的一个,即使我尝试更改色标尺,它也会同时应用于所有地图。有没有办法为不同的面设置不同的颜色比例?
fig = px.choropleth(df,
geojson=counties,
locations='id',
color='count',
facet_col='age_group',
facet_col_wrap=2,
color_continuous_scale='BuGn',
hover_name='county',
width=1000,
height=900,
animation_frame='year')
fig.update_geos(fitbounds="locations")
fig.show()
- 您尚未提供示例 geojson 或数据。选择了美国各州并模拟了一个数据框,因此您的代码几乎可以正常工作(添加 featurekeyid 参数)
- 这是更新图形和动画帧中的轨迹以使用单独的 coloraxis
的情况
- 跟踪已更新,需要设置每个色轴的定位和色阶
import plotly.express as px
import requests
import geopandas as gpd
# get some geojson
geojson = requests.get(
"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_1_states_provinces.geojson"
).json()
counties = {
k: v
if k != "features"
else [
{
k: v if k != "properties" else {"id": i, "name": v["name"]}
for k, v in f.items()
}
for i, f in enumerate(v)
]
for k, v in geojson.items()
}
# construct a dataframe of strucrture implied in question
df = (
pd.json_normalize(counties["features"])
.pipe(lambda d: d.drop(columns=[c for c in d.columns if not "properties" in c]))
.rename(columns={"properties.id": "id", "properties.name": "county"})
.merge(pd.DataFrame({"year": range(2015, 2023)}), how="cross")
.merge(pd.DataFrame({"age_group": ["<18", "18-30", "30-65", ">65"]}), how="cross")
.pipe(
lambda d: d.assign(
count=np.random.randint(1, 50, len(d))
* (pd.factorize(d["age_group"])[0] + 1)
)
)
)
fig = px.choropleth(
df,
geojson=counties,
locations="id",
featureidkey="properties.id",
color="count",
facet_col="age_group",
facet_col_wrap=2,
color_continuous_scale="BuGn",
hover_name="county",
width=1000,
height=900,
animation_frame="year",
)
fig.update_geos(fitbounds="locations")
# update traces to use different coloraxis
for i, t in enumerate(fig.data):
t.update(coloraxis=f"coloraxis{i+1}")
for fr in fig.frames:
# update each of the traces in each of the animation frames
for i, t in enumerate(fr.data):
t.update(coloraxis=f"coloraxis{i+1}")
# position / config all coloraxis
fig.update_layout(
coloraxis={"colorbar": {"x": -0.2, "len": 0.5, "y": 0.8}},
coloraxis2={
"colorbar": {
"x": 1.2,
"len": 0.5,
"y": 0.8,
},
"colorscale": fig.layout["coloraxis"]["colorscale"],
},
coloraxis3={
"colorbar": {"x": -0.2, "len": 0.5, "y": 0.3},
"colorscale": fig.layout["coloraxis"]["colorscale"],
},
coloraxis4={
"colorbar": {"x": 1.2, "len": 0.5, "y": 0.3},
"colorscale": fig.layout["coloraxis"]["colorscale"],
},
)
我制作了一个具有连续色标的等值线图并将其划分为 4 个子图面。问题是色标尺是所有 4 张地图的一个,即使我尝试更改色标尺,它也会同时应用于所有地图。有没有办法为不同的面设置不同的颜色比例?
fig = px.choropleth(df,
geojson=counties,
locations='id',
color='count',
facet_col='age_group',
facet_col_wrap=2,
color_continuous_scale='BuGn',
hover_name='county',
width=1000,
height=900,
animation_frame='year')
fig.update_geos(fitbounds="locations")
fig.show()
- 您尚未提供示例 geojson 或数据。选择了美国各州并模拟了一个数据框,因此您的代码几乎可以正常工作(添加 featurekeyid 参数)
- 这是更新图形和动画帧中的轨迹以使用单独的 coloraxis 的情况
- 跟踪已更新,需要设置每个色轴的定位和色阶
import plotly.express as px
import requests
import geopandas as gpd
# get some geojson
geojson = requests.get(
"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_1_states_provinces.geojson"
).json()
counties = {
k: v
if k != "features"
else [
{
k: v if k != "properties" else {"id": i, "name": v["name"]}
for k, v in f.items()
}
for i, f in enumerate(v)
]
for k, v in geojson.items()
}
# construct a dataframe of strucrture implied in question
df = (
pd.json_normalize(counties["features"])
.pipe(lambda d: d.drop(columns=[c for c in d.columns if not "properties" in c]))
.rename(columns={"properties.id": "id", "properties.name": "county"})
.merge(pd.DataFrame({"year": range(2015, 2023)}), how="cross")
.merge(pd.DataFrame({"age_group": ["<18", "18-30", "30-65", ">65"]}), how="cross")
.pipe(
lambda d: d.assign(
count=np.random.randint(1, 50, len(d))
* (pd.factorize(d["age_group"])[0] + 1)
)
)
)
fig = px.choropleth(
df,
geojson=counties,
locations="id",
featureidkey="properties.id",
color="count",
facet_col="age_group",
facet_col_wrap=2,
color_continuous_scale="BuGn",
hover_name="county",
width=1000,
height=900,
animation_frame="year",
)
fig.update_geos(fitbounds="locations")
# update traces to use different coloraxis
for i, t in enumerate(fig.data):
t.update(coloraxis=f"coloraxis{i+1}")
for fr in fig.frames:
# update each of the traces in each of the animation frames
for i, t in enumerate(fr.data):
t.update(coloraxis=f"coloraxis{i+1}")
# position / config all coloraxis
fig.update_layout(
coloraxis={"colorbar": {"x": -0.2, "len": 0.5, "y": 0.8}},
coloraxis2={
"colorbar": {
"x": 1.2,
"len": 0.5,
"y": 0.8,
},
"colorscale": fig.layout["coloraxis"]["colorscale"],
},
coloraxis3={
"colorbar": {"x": -0.2, "len": 0.5, "y": 0.3},
"colorscale": fig.layout["coloraxis"]["colorscale"],
},
coloraxis4={
"colorbar": {"x": 1.2, "len": 0.5, "y": 0.3},
"colorscale": fig.layout["coloraxis"]["colorscale"],
},
)