Plotly中的Choropleth贴图:颜色显示不正确

时间:2018-12-08 19:01:50

标签: python pandas plotly data-visualization choropleth

尝试使用我在csv文件中拥有的一些数据以绘图方式制作Choropleth映射。创建了以下地图:

my choromap

但是,这不是正确的数据显示。这是我的csv文件的摘录:

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

基于此,我希望中国的颜色与美国所加载的颜色相似,但是看起来与价值小于200的国家相同。有人知道这是什么原因吗?

这是我的完整代码供参考:

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 个答案:

答案 0 :(得分:0)

根据您的评论,我将确保中国的确是2447,而不是244。虽然您的示例代码可行,但我也将遵循plotly documentation

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

,或者如果要脱机打印,则:

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

如果您使用iplot,则可以编辑图表并以图形方式查看数据,以确保数据看起来正确