1
votes

I've been having trouble with a world choropleth map using plotly in Jupyter. Once the value goes above 1000, the map grays out that country as id there is no data. I double checked the country codes, and they are all correct. I'm thinking the problem has to do with the legend because it only shows up to 1000, but I'm not sure how to change that. I followed the docs at https://plot.ly/python/choropleth-maps/ to get where I am right now. Any help would be appreciated. Here's a snapshot of my data

        number  CODE   COUNTRY
     0  1146    USA    United States
     1  1450    CAN    Canada
     2  54      CRI    Costa Rica
     3  920     AUS    Australia

Here's my code so far

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()

And here's the result (map is sorta cut off, but you get the idea) Plot

1

1 Answers

1
votes

I'm pretty certain that the problem lies in your data source. You've either got a bad dataformat for number, or you've got missing data for some countries, or even countries that are completely missing in your COUNTRY column.

Why am I certain?

Take a look at the following plot produced by the snippet below:

Plot:

enter image description here

Code:

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()

With the information available to me so far, a couple of things come to mind:

  1. Grey is not part of the color scale.
  2. A few countries such as Greenland and Russia are grey.
  3. These countries are also missing in the data source.

gapminder['country'].unique() returns:

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)

I suggest you take a closer look at your dataset using tools such df['CODE'].unique() and let us know what you find.