英文:
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()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论