plotting data on to a country map

449 Views Asked by At

hey guys having trouble with vincent, im not sure exactly how to use it So ive parsed some data from the UK house of commons petition site, and now have a list of countries and their corresponding number of votes into a certain petition and ive got the data from JSON to ('Austria', 40) format

Im using vincent to plot them onto a map with colour scaled to represent number of votes but dont really know how to use vincent

for example to render a basic map of the world the code is

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')

but that just outputs a JSON, not a picture of a map, even though that is what two tutorial examples are saying should happen (for example here: http://wrobstory.github.io/2013/10/mapping-data-python.html and another place I forgot to save the link)

could someone help me out? thanks in advance guys

2

There are 2 best solutions below

0
On

If you want to see a picture of a map you will have to create a html file that will read the json file and map it.

<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>

Then open your command line and type in: Python -m SimpleHTTPServer 8000 # Python 2 Next open your browser at http://localhost:8000/path/to/json/file.

You should now see a map on the page. Note this will be a basic map depending on what data you passed from the json file.

I hope this helps and good luck!

0
On

First you should change the last line of your code. Try this simplified example:

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')

Then using the terminal CD to the location of your project, i.e. where vega.html is saved.

After that run a local server using Python -m SimpleHTTPServer 8000. After that you can open any browser and type http://localhost:8000/vega.html .

Note that the depending on the version of vincent the notation inside the .tojson may be different.

Hope that helps:)

P.S. I think you should add the Python tag as well so people can find it easier.