You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly Choropleth地图无法填充数据问题求助

Troubleshooting Blank Plotly Express US States Choropleth Map

Here are the most common fixes for your blank map issue:

  • Ensure state abbreviations are uppercase: Plotly's locationmode='USA-states' strictly requires uppercase 2-letter state codes (e.g., 'CA', 'TX'). Mixed or lowercase codes won't be recognized. Fix this with:

    df['state'] = df['state'].str.upper()
    
  • Validate state codes for errors: Check for missing values, typos, or non-standard abbreviations (like 'NYC' instead of 'NY'). Use this to inspect unique state values:

    print(df['state'].unique())
    

    Filter out any invalid entries before plotting.

  • Double-check parameter mappings: Confirm your px.choropleth call correctly maps columns to parameters:

    fig = px.choropleth(
        df,
        locations='state',  # Must match your state column name exactly
        locationmode='USA-states',
        color='Users',      # Must match your user count column name
        scope='usa'
    )
    
  • Verify non-zero user counts: If all values in the Users column are zero, the map will appear blank (no color variation). Check with:

    print(df['Users'].describe())
    
  • Update Plotly: Older versions of Plotly might have bugs with US state choropleths. Update to the latest version:

    pip install --upgrade plotly
    

Example Corrected Code

import plotly.express as px
import pandas as pd

# Load data
df = pd.read_csv('your_data.csv')

# Clean state codes
df['state'] = df['state'].str.upper()
df = df[df['state'].str.len() == 2]  # Filter out invalid codes

# Create map
fig = px.choropleth(
    df,
    locations='state',
    locationmode='USA-states',
    color='Users',
    scope='usa',
    title='US State User Counts'
)
fig.show()

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:04:58