python - Choropleth 不绘制任何内容

标签 python pandas plot plotly

我想使用plotly和Choroplet根据某些国家的人口流动情况显示世界地图,但它不起作用,我不明白为什么。

This is my pandas table

这段代码应该绘制图表:

import plotly.plotly as py
import plotly
import plotly.figure_factory as ff
from plotly.offline import download_plotlyjs, init_notebook_mode, plot,iplot
init_notebook_mode(connected=True)
flow = color_table.iloc[:,3]

data = [dict(type = 'choroplet',
         colorscale = 'Rainbow',
         location = color_table['COUNTRY_NAME_IT_y'],
         z = flow,
         text = color_table['COUNTRY_NAME_IT_x'],
         colorbar = dict(title = 'Flow of foreign users', titlefont=dict(size=25),tickfont=dict(size=18)),
        )]


layout = dict(title = 'Flow of foreign users',
         geo = dict(showframe = False,showcoastlines = False,projection = dict(type = 'equirectangular'))
         )

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

最佳答案

经过一些更改后,我终于可以启动你的分区统计表了:

1.type='choropleth',而不是type='choroplet'

2.“位置”,而不是“位置”

3.如果你想要世界地图,那么将 showcoaSTLines 更改为 True - 这会让 map 看起来更漂亮。

4.您可以删除geo中的projection,因为type=equirectangular是该参数的默认值。

我添加了具有默认值的 locationmodescope 参数(但对两者都进行了注释)。这些参数在构建分区统计图时会对您有很大帮助。例如,将 scope='world' 更改为 scope='europe' 允许您仅看到欧洲 map 而不是世界(在您的数据中所有国家/地区都来自欧洲,因此将也许有道理)。您可以阅读有关这些参数的更多信息 12 。另外,不要害怕查看有关 choropleth 和 geo 模块的所有参数的完整引用 34

代码:

from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)

list1 = ['Romania', 'Russia', 'Austria']
list2 = ['ROU', 'RUS', 'AUT']
list3 = [0.4, 0.1, 0.3]

data = [dict(type='choropleth',
             colorscale='Rainbow',
             locations=list2,
             # locationmode='ISO-3',
             z=list3,
             text=list1,
             colorbar=dict(title='Flow of foreign users',
                           titlefont=dict(size=25),
                           tickfont=dict(size=18)),
             )]


layout = dict(title='Flow of foreign users',
              geo=dict(
                       showframe=False,
                       showcountries=True,
                       # scope='world',
                       ),
              )

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

输出:

World Map Choropleth

关于python - Choropleth 不绘制任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726058/

相关文章:

python - 如何获取任何网站的域名?

python - 使用 'in' 运算符在 Pandas 数据帧上执行 bool 掩码的 Pythonic 方法是什么

python - 创建具有连续字符串计数的矩阵

python - 如何获取多索引数据帧与引用值的偏差

r - ggplot : no color in plot

r - 绘制 xts 对象 - 为 lwd 和 col 参数传递值创建错误

python - TensorFlow 是否允许定义未定义大小的变量?

python - 如何在 nltk 中将自定义语料库添加到本地计算机

python - 在 gensim python 中使用 google word2vec .bin 文件

python - 以百分比和计数值作为 pandas DF 中标签的马赛克图