本文介绍了为什么Python Vincent map visuzalization不能映射Data Frame中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python vincent地图可视化,并且使用了此软件包入门示例.我在ipython notebook工作.

I am using Python vincent map visualization with the use of this package introductory examples. I work in ipython notebook.

我用国家FIPS代码定义了简单的pandas DataFrame(摘自此处).然后,我尝试通过这些FIPS代码将DataFrame数据与vincent映射进行映射,但是导致的可视化无法以任何方式为国家/地区着色.我该如何运作?

I defined simple pandas DataFrame with country FIPS codes (taken from here). Then I tried to map DataFrame data with vincent map by these FIPS codes, but resulted visualization fails to colour countries in any manner. How can I make it work?

country_data_tmp = pd.DataFrame({'country_names' : np.array(['Argentina', 'Armenia', 'Australia', 'Austria']),
                                 'country_FIPS' : np.array(['032', '051', '036', '040']),
                                 'my_rate' : np.array([0.254, 0.3456, 0.26, 0.357])})
country_data_tmp.head()
world_topo = r'world-countries.topo.json'

geo_data = [{'name': 'countries',
             'url': world_topo,
             'feature': 'world-countries'}]

vis = vincent.Map(data=country_data_tmp, 
                  geo_data=geo_data, 
                  scale=1100, 
                  data_bind='my_rate', 
                  data_key='country_FIPS',
                  map_key={'counties': 'properties.FIPS'})

vis.display()

推荐答案

由于您未正确设置map_key,因此它们不会显示. world_countries.topo.json文件通过3个字母代码标识该国家/地区,该文件中名为id(这与您链接到的页面).如果您查看该json中的原始数据,就可以看到此内容文件.

They don't display because you have not set the map_key correctly. The world_countries.topo.json file identifies the countries by 3 letter code, named id in that file (this corresponds to the field called alpha-3 in the page you linked to). You can see this if you look at the raw data in that json file.

此外,您在geo_data中设置了'name': 'countries',但是在map_key中,您尝试将其引用为counties(请注意缺少的r).很容易犯错,因为示例页面中的counties是他们在绘制美国县的地图.

Also, you set 'name': 'countries' in geo_data, but in map_key you try to reference it as counties (note the missing r). Easy mistake to make, as it's counties in the example page where they're mapping US counties.

如果更改变量名称以使它们引用非空字段-您将获得一个漂亮的映射,因为数据表中的country_alpha3与JSON变量countries中的id匹配.

If you change the variable names so that they reference non-empty fields - you'll get a lovely map as country_alpha3 in your data table matches id in the JSON variable countries.

N.B. .按照您的代码所示,只会绘制您拥有数据的国家/地区.您可以在此处的第二个示例中添加带有此处的所有国家/地区轮廓的图层,如果您只想概述所有内容,则仅列出带有彩色数据的内容.我在下面的第二个代码/输出部分中提供了对代码的更改.

N.B. As your code stands, only the countries for which you have data will be plotted. You could add a layer with all country outlines as per the second example here if you want all outlined, but only the ones with data coloured. I've provided changes to the code to do that in the second code / output section below.

N.B. 2 在当前值为my_rate的情况下,颜色对比度不是很明显.用[0,0.3,0.7,1.0]尝试一下,以使自己确信它们的着色是不同的.

N.B. 2 With your current values of my_rate the colour contrast is not very noticeable. Try it out with [0,0.3,0.7,1.0] to convince yourself it is colouring them differently.

#Data setup bit - Input[1] from your notebook
#Note new name for country code country_alpha3

import pandas as pd
import numpy as np

country_data_tmp = pd.DataFrame({'country_names' : np.array(['Argentina', 'Armenia', 'Australia', 'Austria']),
                                 'country_alpha3' : np.array(['ARG','ARM','AUS','AUT']),
                                 'my_rate' : np.array([0.254, 0.3456, 0.26, 0.357])})
country_data_tmp.head()

#map drawing bit Input[2] from your notebook
#Note the changes in variable names

world_topo = r'world-countries.topo.json'

geo_data = [{'name': 'countries',
             'url': world_topo,
             'feature': 'world-countries'}]

vis = vincent.Map(data=country_data_tmp, 
                  geo_data=geo_data, 
                  scale=1100, 
                  data_bind='my_rate', 
                  data_key='country_alpha3',
                  map_key={'countries': 'id'})

vis.display()

输出

#Replace input[2] with this to add a layer with outline only

world_topo = r'world-countries.topo.json'

geo_data = [{'name': 'countries',
             'url': world_topo,
             'feature': 'world-countries'},
           {'name': 'countries_outline',
             'url': world_topo,
             'feature': 'world-countries'}]

vis = vincent.Map(data=country_data_tmp, 
                  geo_data=geo_data, 
                  scale=100, 
                  data_bind='my_rate', 
                  data_key='country_alpha3',
                  map_key={'countries': 'id'})

del vis.marks[1].properties.update
vis.marks[1].properties.enter.stroke.value = '#000'

vis.display()

输出(输出层加数据层)

这篇关于为什么Python Vincent map visuzalization不能映射Data Frame中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 23:39