Python 在特定值后,将切掉切块

Python 在特定值后,将切掉切块,python,plotly,choropleth,Python,Plotly,Choropleth,我在Jupyter使用plotly时遇到了一个世界choropleth地图的问题。一旦该值超过1000,地图将灰显该国家,因为没有数据。我仔细检查了国家代码,它们都是正确的。我认为这个问题与传说有关,因为它最多只能显示1000个,但我不知道如何改变它。我跟着医院的医生走到了我现在的位置。任何帮助都将不胜感激。 这是我的数据快照 number CODE COUNTRY 0 1146 USA United States 1 1450 C

我在Jupyter使用plotly时遇到了一个世界choropleth地图的问题。一旦该值超过1000,地图将灰显该国家,因为没有数据。我仔细检查了国家代码,它们都是正确的。我认为这个问题与传说有关,因为它最多只能显示1000个,但我不知道如何改变它。我跟着医院的医生走到了我现在的位置。任何帮助都将不胜感激。 这是我的数据快照

        number  CODE   COUNTRY
     0  1146    USA    United States
     1  1450    CAN    Canada
     2  54      CRI    Costa Rica
     3  920     AUS    Australia
这是到目前为止我的代码

fig = go.Figure(data=go.Choropleth(
    locations = df['CODE'],
    locationmode = "ISO-3",
    z = df['number'],
    text = df['COUNTRY'],
    colorscale = 'Reds'
))

fig.update_layout(title_text="McDonald's per Country")

fig.show()
结果如下(地图被切断了,但你明白了)

我非常确定问题在于您的数据源。您的
number
数据格式不正确,或者某些国家的数据丢失,甚至在
COUNTRY
列中完全丢失的国家

为什么我能肯定

请看下面的代码段生成的以下绘图:

绘图:

import plotly.express as px

gapminder = px.data.gapminder().query("year==2007")
fig = px.choropleth(gapminder, locations="iso_alpha",
                    color="lifeExp", # lifeExp is a column of gapminder
                    hover_name="country", # column to add to hover information
                    color_continuous_scale=px.colors.sequential.Plasma)
fig.show()

代码:

import plotly.express as px

gapminder = px.data.gapminder().query("year==2007")
fig = px.choropleth(gapminder, locations="iso_alpha",
                    color="lifeExp", # lifeExp is a column of gapminder
                    hover_name="country", # column to add to hover information
                    color_continuous_scale=px.colors.sequential.Plasma)
fig.show()
到目前为止,我所掌握的信息让我想到了几件事:

  • 灰色
    不是色阶的一部分
  • 一些国家,如
    格陵兰
    俄罗斯
    是灰色的
  • 数据来源中也缺少这些国家
  • gapminder['country'].unique()
    返回:

    array(['Afghanistan', 'Albania', 'Algeria', 'Angola', 'Argentina',
           'Australia', 'Austria', 'Bahrain', 'Bangladesh', 'Belgium',
           'Benin', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil',
           'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon',
           'Canada', 'Central African Republic', 'Chad', 'Chile', 'China',
           'Colombia', 'Comoros', 'Congo, Dem. Rep.', 'Congo, Rep.',
           'Costa Rica', "Cote d'Ivoire", 'Croatia', 'Cuba', 'Czech Republic',
           'Denmark', 'Djibouti', 'Dominican Republic', 'Ecuador', 'Egypt',
           'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Ethiopia',
           'Finland', 'France', 'Gabon', 'Gambia', 'Germany', 'Ghana',
           'Greece', 'Guatemala', 'Guinea', 'Guinea-Bissau', 'Haiti',
           'Honduras', 'Hong Kong, China', 'Hungary', 'Iceland', 'India',
           'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy',
           'Jamaica', 'Japan', 'Jordan', 'Kenya', 'Korea, Dem. Rep.',
           'Korea, Rep.', 'Kuwait', 'Lebanon', 'Lesotho', 'Liberia', 'Libya',
           'Madagascar', 'Malawi', 'Malaysia', 'Mali', 'Mauritania',
           'Mauritius', 'Mexico', 'Mongolia', 'Montenegro', 'Morocco',
           'Mozambique', 'Myanmar', 'Namibia', 'Nepal', 'Netherlands',
           'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway', 'Oman',
           'Pakistan', 'Panama', 'Paraguay', 'Peru', 'Philippines', 'Poland',
           'Portugal', 'Puerto Rico', 'Reunion', 'Romania', 'Rwanda',
           'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia',
           'Sierra Leone', 'Singapore', 'Slovak Republic', 'Slovenia',
           'Somalia', 'South Africa', 'Spain', 'Sri Lanka', 'Sudan',
           'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Taiwan',
           'Tanzania', 'Thailand', 'Togo', 'Trinidad and Tobago', 'Tunisia',
           'Turkey', 'Uganda', 'United Kingdom', 'United States', 'Uruguay',
           'Venezuela', 'Vietnam', 'West Bank and Gaza', 'Yemen, Rep.',
           'Zambia', 'Zimbabwe'], dtype=object)
    

    我建议您使用
    df['code'].unique()等工具仔细查看您的数据集,并让我们知道您发现了什么。

    您是正确的,这只是我的数据框架中的一个疏忽。在“数字”列中,一旦值超过1000,它们就被写为“1446”,被当作字符串处理,并被跳过。我最后做了:
    对于范围内的索引(len(df[“number”]):df.number[index]=pd.to_numeric(df.number[index]。replace(“,”,”)
    ,这就解决了它。事实上,在我的报告中,有更多的国家和非洲一样,都是灰色的,因为没有数据,所以我放弃了这些数据。我有101个独特的countries@JackHanson很高兴听到这个消息!如果我的建议是有用的,你会考虑把它作为可接受的答案吗?