简体   繁体   中英

Plotly Choropleth

I am working with a Kaggle dataset "US Accidents" (which can be downloaded here ) that has 3 million records on traffic accident data. A quick exploration shows that California contains the most accidents. I thought a choropleth viz would be cool to implement however, the data on my Choropleth is inaccurate and was wondering where I am going wrong/how to fix it. 在此处输入图像描述在此处输入图像描述

Here is my code...

states_by_accident = df.State.value_counts()

import plotly.graph_objects as go
fig = go.Figure(data = go.Choropleth(
    locations = df.State.unique(),
    z = states_by_accident,
    locationmode = 'USA-states',
    colorscale = 'Blues'
))


fig.update_layout(
       geo_scope = 'usa'
   )
fig.show()

I have tried converting the colors to a log scale which helped spread out the coloring but it still displayed Ohio as having the most accidents which is inaccurate.

This is happening because df.State.unique() doesn't have the states in the same order as the values in states_by_accident .

You can fix this by instead passing the argument locations = states_by_accident.index to go.Chloropleth so that the locations and values are consistent:

fig = go.Figure(data = go.Choropleth(
    locations = states_by_accident.index,
    z = states_by_accident,
    locationmode = 'USA-states',
    colorscale = 'Blues'
))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM