1
votes

Trying to make a choropleth map in plotly using some data I have in a csv file. Have created the following map:

my choromap

This isn't a correct display of the data however. Here is an excerpt of my csv file:

China,2447
...
Trinidad And Tobago,2
Turkey,26
Ukraine,8
United Arab Emirates,97
United States of America,2008

Based on this I'd expected China to appear in a similar colour to that which the US has loaded in, however it looks the same as countries with values of less than 200. Does anyone know what the reason for this is?

Here's my full code for reference:

import pandas as pd
import plotly as py 

df = pd.read_csv('app_country_data_minus_uk.csv')

data = [dict(type='choropleth',
                locations = df['Country'],
                locationmode = 'country names',
                z = df['Applications'],
                text = df['Country'],
                colorbar = {'title':'Apps per country'},
                colorscale = 'Jet',
                reversescale = False
                )]


layout = dict(title='Application Jan-June 2018',
geo = dict(showframe=False,projection={'type':'mercator'}))

choromap = dict(data = data,layout = layout)
red = py.offline.plot(choromap,filename='world.html')
1
I am unable to reproduce the issue with your example - It_is_Chris
Are there any steps you would suggest for trying to fix this? - PalacesOnMonday

1 Answers

0
votes

per your comment I would make sure that china is indeed 2447 and not something like 244. I would also follow the plotly documentation although you example code works.

import plotly.plotly as py
import pandas as pd

df = pd.read_csv('app_country_data_minus_uk.csv')

data = [ dict(
        type = 'choropleth',
        locations = df['Country'],
        locationmode = 'country names',
        z = df['Applications'],
        colorscale = 'Jet',
        reversescale = False,
        marker = dict(
            line = dict (
                color = 'rgb(180,180,180)',
                width = 0.5
            ) ),
        colorbar = dict(
            autotick = False,
            tickprefix = '',
            title = 'Apps per country'),
      ) ]

layout = dict(
    title = 'app_country_data_minus_uk',
    geo = dict(
        showframe = True,
        showcoastlines = True,
        projection = dict(
            type = 'Mercator'
        )
    )
)

fig = dict( data=data, layout=layout )
py.iplot( fig, validate=False, filename='d3-world-map' )

enter image description here

or if you want to plot it offline:

import plotly.plotly as py
import pandas as pd
import plotly

df = pd.read_csv('app_country_data_minus_uk.csv')

data = [ dict(
        type = 'choropleth',
        locations = df['Country'],
        locationmode = 'country names',
        z = df['Applications'],
        colorscale = 'Jet',
        reversescale = False,
        marker = dict(
            line = dict (
                color = 'rgb(180,180,180)',
                width = 0.5
            ) ),
        colorbar = dict(
            title = 'Apps per country'),
      ) ]

layout = dict(
    title = 'app_country_data_minus_uk',
    geo = dict(
        showframe = True,
        showcoastlines = True,
        projection = dict(
            type = 'Mercator'
        )
    )
)

fig = dict( data=data, layout=layout )
plotly.offline.plot(fig,filename='world.html')

If you use iplot you will be able to edit the chart and see the data in plotly to make sure your data looks correct