Skip to main content
 首页 » 编程设计

python之为什么 Python Vincent map 可视化不映射来自 Data Frame 的数据

2024年08月25日29zhoujg

我正在使用 Python vincent map 可视化和这个包 introductory examples 。我在 ipython notebook 中工作。

我使用国家/地区 FIPS 代码(取自 here)定义了简单的 pandas DataFrame。然后,我尝试通过这些 FIPS 代码将 DataFrame 数据映射到 vincent 映射,但生成的可视化无法以任何方式为国家/地区着色。我怎样才能让它发挥作用?

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(这对应于名为 alpha-3 的字段> 在 page you linked to 中)。如果您查看 the raw data in that json file ,您可以看到这一点。

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

如果您更改变量名称以便它们引用非空字段 - 您将得到一张漂亮的 map ,因为数据表中的 country_alpha3 与 JSON 中的 id 匹配变量国家/地区。

N.B. 根据您的代码,只会绘制您拥有数据的国家/地区。如果你想要所有的轮廓,你可以添加一个包含所有国家轮廓的图层作为 per the second example here,但只有那些有颜色的数据。我在下面的第二个代码/输出部分中提供了代码更改以执行此操作。

注意2 使用您当前的 my_rate 值,颜色对比度不是很明显。使用 [0,0.3,0.7,1.0] 尝试一下,让自己相信它正在为它们着色。

代码

#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() 

输出(输出层加数据层)