1

嘿伙计们,文森特有问题,我不确定如何使用它所以我从英国下议院请愿网站解析了一些数据,现在有一个国家列表和他们在某个请愿书中的相应票数,我得到了从 JSON 到('Austria', 40)格式的数据

我使用 vincent 将它们绘制到地图上,颜色缩放以表示票数,但真的不知道如何使用 vincent

例如,要渲染世界的基本地图,代码是

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

vis = Map(geo_data=geo_data, scale=200)
vis.to_json('vega.json')

但这只是输出 JSON,而不是地图图片,即使这是两个教程示例所说的应该发生的情况(例如这里:http ://wrobstory.github.io/2013/10/mapping-data-python .html和另一个我忘记保存链接的地方)

有人可以帮帮我吗?提前谢谢你们

4

2 回答 2

0

如果您想查看地图图片,则必须创建一个 html 文件,该文件将读取 json 文件并对其进行映射。

<html>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/d3.geo.projection.v0.min.js" charset="utf-8"></script>
<script src="http://trifacta.github.com/vega/vega.js"></script>
</head>

<body>
<div id="vis"></div>
//This script will read your json file and print it to the map
<script type="text/javascript">
    function parse(spec)
    {
        vg.parse.spec(spec, function(chart) { chart({el:"#vis"}).update(); });
    }
    //Put the name of your json file where vega.json is
    parse("vega.json");
</script>
</body>
</html>

然后打开你的命令行并输入: Python -m SimpleHTTPServer 8000 # Python 2 接下来打开你的浏览器在http://localhost:8000/path/to/json/file.

您现在应该在页面上看到一张地图。请注意,这将是一个基本映射,具体取决于您从 json 文件传递​​的数据。

我希望这个帮助能祝你好运!

于 2016-06-27T15:41:26.827 回答
0

首先,您应该更改代码的最后一行。试试这个简化的例子:

import vincent
list_data = [10, 20, 30, 20, 15, 30, 45]
vega = vincent.Bar(list_data)
vega.to_json('vega.json',html_out=True,html_path='vega.html')

然后使用终端 CD 到您的项目的位置,即保存 vega.html 的位置。

之后使用Python -m SimpleHTTPServer 8000. 之后,您可以打开任何浏览器并输入http://localhost:8000/vega.html

请注意,根据 vincent 的版本,内部的符号.tojson可能会有所不同。

希望有帮助:)

PS 我认为您也应该添加 Python 标签,以便人们更容易找到它。

于 2016-07-11T19:24:35.190 回答