如何使法国在此代码生成的地图中完全透明?

问题描述 投票:0回答:1

我正在使用此代码创建欧洲地图,我希望法国在某些视图中完全不可见,我不想将其从国家/地区列表中删除。更改不透明度不会改变结果。

基本上我想改变个别国家的不透明度。

enter image description here

import pandas as pd
import plotly.express as px
import kaleido
from plotly.graph_objs import layout

df_france = pd.DataFrame({
    "country": ["France"]
})

df_ukraine = pd.DataFrame({
    "country": ["Ukraine"]
})

df = pd.concat([df_france, df_ukraine], ignore_index=True)

fig = px.choropleth(df, locations="country",
                    locationmode="country names",
                    color="country",
                    scope="europe")

fig.update_geos(
    resolution=50,
    fitbounds="locations",
    showcountries=False,
    countrycolor="#2d32aa"
    )

fig.update_layout(width=1920, height=1080)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}, showlegend=False)
fig.update_traces(selector=dict(country="Ukraine"), marker=dict(opacity=0.7))
fig.update_traces(selector=dict(location="France"), marker=dict(opacity=0.3))

fig.update_layout(margin={"l": 0, "b": 0, "r": 0, "t": 0})
fig.update_layout(width=1935, height=1840)

# Show the plot
fig.show()
python plotly plotly-express
1个回答
0
投票

在调用

.update_traces()
方法时使用了错误的选择器:
country
location
不是有效属性,但
locations
是(参见 Python 图参考:Choropleth Traces)。

示例:

fig.update_traces(selector=dict(locations=["Ukraine"]), marker=dict(opacity=0.7))
fig.update_traces(selector=dict(locations=["France"]), marker=dict(opacity=0.3))

如果您想在地图上完全忽略法国,即将其从

layout.geo.fitbounds
功能中排除(设置为
"locations"
),该功能会自动缩放地图以仅显示感兴趣的区域),请使用
visible
属性:

fig.update_traces(selector=dict(locations=["France"]), visible=False)
© www.soinside.com 2019 - 2024. All rights reserved.