【发布时间】:2017-10-28 15:53:52
【问题描述】:
我想制作一个等值线世界地图,在世界地图上显示一个单词的点击量(搜索次数)。
以下是代码:
import plotly
import plotly.offline
import pandas as pd
df = pd.read_excel('F:\\Intern\\csir\\1yr\\news\\region_2016_2017.xlsx')
df = df.query('keyword==["addiction"]')
scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
[0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
data = [dict(
type='choropleth',
colorscale=scl,
locations = df['location'],
z = df['hits'].astype(int),
locationmode = "country names",
autocolorscale = False,
reversescale = False,
marker = dict(
line = dict (
color = 'rgb(180,180,180)',
width = 0.5)),
colorbar = dict(
autotick = False,
title = 'Hits'),)]
layout = dict(
title = 'Addiction keyword 1yr analysis',
geo = dict(
showframe = False,
showcoastlines = False,
projection = dict(
type = 'Mercator'
)
)
)
fig = dict(data = data,layout = layout)
plotly.offline.plot(fig,validate=False,filename = 'd3-world-map.html')
可以清楚地看到,许多国家都不见了。这可能是因为许多国家/地区没有明确声明其命中率为零的条目。
我不想用我的数据明确地这样做。有没有其他方法可以解决这个问题?这样我们就可以看到所有国家/地区。
数据集可以在here找到。
请注意,我链接的数据集是 .csv 文件,而程序中使用的文件是文件的 .xlsx 版本。
【问题讨论】:
标签: python plotly choropleth