Plotly express Choropleth突出显示特定州

huangapple go评论77阅读模式
英文:

Plotly express Choropleth highlight specific states

问题

我已生成了一个使用Plotly Express创建的区域地图。我正在绘制每个州的人口,并希望突出显示人口最多和人口最少的州。是否有一种方法可以更改特定州的边框颜色?

当前代码:

df = pd.read_csv("state_pop.csv")

fig = px.choropleth(df,
                    locations="state",
                    scope="usa",
                    color="population",
                    locationmode="USA-states",
                    color_continuous_scale="blues",
                    )
fig.show()
英文:

I have generated a choropleth using plotly express. I am plotting the population of each state and I would like to highlight the state with the largest population and the smallest population. Is there a way to change the boarder color for a specific state?

current code:

df = pd.read_csv("state_pop.csv")

fig = px.chorolpeth(df,
                    locations="state",
                    scope="usa",
                    color="population",
                    locationmode="USA-states",
                    color_continuous_scale="blues",
                    )
fig.show()

答案1

得分: 1

I found similar data in the reference and used it to change the color of the boundaries between the states with the largest and smallest export value. The coloring order is drawn alphabetically by state abbreviation, so please specify the index of maximum and minimum in your data

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

color_list = ['blue' if x == 2 or x == 5 else 'white' for x in range(1, len(df) + 1, 1)]

fig = go.Figure(data=go.Choropleth(
    locations=df['code'],
    z=df['total exports'].astype(float),
    locationmode='USA-states',
    colorscale='Reds',
    autocolorscale=False,
    marker_line_color=color_list,
    colorbar_title="Millions USD"
))

fig.update_layout(
    height=500,
    title_text='2011 US Agriculture Exports by State',
    geo_scope='usa',  # limit map scope to USA
)

fig.show()

Plotly express Choropleth突出显示特定州

英文:

I found similar data in the reference and used it to change the color of the boundaries between the states with the largest and smallest export value. The coloring order is drawn alphabetically by state abbreviation, so please specify the index of maximum and minimum in your data

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

color_list = ['blue' if x == 2 or x == 5 else 'white' for x in range(1,len(df)+1,1)]

fig = go.Figure(data=go.Choropleth(
    locations=df['code'],
    z=df['total exports'].astype(float),
    locationmode='USA-states',
    colorscale='Reds',
    autocolorscale=False,
    marker_line_color=color_list, 
    colorbar_title="Millions USD"
))


fig.update_layout(
    height=500,
    title_text = '2011 US Agriculture Exports by State',
    geo_scope='usa', # limite map scope to USA
)

fig.show()

Plotly express Choropleth突出显示特定州

huangapple
  • 本文由 发表于 2023年5月13日 09:09:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定