【问题标题】:Plotly Choropleth in pandasPlotly 大熊猫的合唱团
【发布时间】:2022-01-12 22:05:44
【问题描述】:

我试图在列 jobLocation 上的我的数据框“json”上做一个 Plotly Choropleth。下面是DataFrame:

绘图显示没有数据。我做错了什么?



fig = px.choropleth(json, locations='jobLocation', locationmode="USA-states", color='jobLocation',
                             scope="usa",
                             labels={'jobLocation':'jobLocation'})
fig.show()

【问题讨论】:

    标签: python pandas plot plotly


    【解决方案1】:

    The Plotly Express documentation 说:

    要使用美国各州几何,请设置 locationmode='USA-states' 和 以两个字母的州缩写形式提供位置

    解决方案:从jobLocation列中的每个字符串中拆分出两个字母的状态缩写。

    根据你的截图,试试这个:

    json['state'] = state['jobLocation'].str.split(', ').str[1]
    

    随后修改您的绘图代码以删除缺少位置的行(Plotly 在我的测试中要求),并使用locations='state'

    fig = px.choropleth(json.dropna(subset=['state']), 
                        locations='state', 
                        locationmode='USA-states', 
                        color='jobLocation',
                        scope='usa',
                        labels={'jobLocation':'jobLocation'})
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2022-06-10
      • 2022-11-28
      • 2020-05-04
      • 2017-02-11
      相关资源
      最近更新 更多