6

I am trying to create a simple program so as to display the map of Central America with its population using Pygal_maps_world. Here's the code for the same:

import pygal_maps_world as pa

wm=pa.World()

wm.title="Map of Central America"
wm.add('North America',{'cd': 84949494949,'mx': 494794164,'us': 99794616})

wm.render_to_file('map.svg')

I have tried a few combinations regarding the importing of the World maps to work properly but to no avail and I am not able to create the visualization.

maciejwww
  • 1,067
  • 1
  • 13
  • 26
Dhruv Marwha
  • 1,064
  • 2
  • 14
  • 26

3 Answers3

8

In the recent versions of pygal, you start with the first four lines of this code:

from pygal.maps.world import World

wm = World()
wm.force_uri_protocol = 'http'

wm.title="Map of Central America"
wm.add('North America',{'ca': 84949494949,'mx': 494794164,'us': 99794616})

wm.render_to_file('map.svg')

When I use those first four lines and the rest of your code, your code runs and it highlights the United States and Mexico, but it highlights the Democratic Republic of the Congo rather than Canada. Change the country code to 'ca' for Canada! This then, of course, now shows North America rather than central America, so either change your title or change your country codes.

enter image description here

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • It says that it cannot find a reference in __init__.py and so it does not run.I am using Anaconda 3 along with Pycharm Community edition on a windows 10 system. – Dhruv Marwha Jun 08 '16 at 14:50
  • @DhruvMarwha: What are the versions of Python and of pygal? I use Anaconda 2 4.0.0, pygal 2.2.3, Spyder 2.3.8, and Python 2.7.11. And please show the entire traceback for the error. – Rory Daulton Jun 08 '16 at 22:29
  • Unfortunatley this isn't working for me in Spyder/Anaconda environment I get `TypeError: argument of type 'Key' is not iterable` when I try to `render_to_file`. Weirdly, when I just run the same code from the terminal, it works fine. So it seems to be a problem with Spyder, not my Anaconda build. (FYI I'm in Python 3.6, Spyder 3.1.4, in Ubuntu 14 lts). – eric Jul 29 '17 at 16:04
  • 1
    @neuronet: [Spyder 3.1.4 has known bugs](https://stackoverflow.com/questions/44933245/python-spyder-ide-3-14-python-3-6-kernel-restarts-while-running-in-debug-mode#comment77516706_44933245), which are so bad that I down-graded to 3.1.3 for a while. Version 3.2.0 is now available through conda: upgrade your Spyder and see if that helps. That certainly helped me. – Rory Daulton Jul 29 '17 at 16:17
  • @RoryDaulton it sort of works. For those wondering, to update Spyder and if you have Conda, just go to your terminal and `conda update spyder`. Strangely, though, it only works once. Once I run the code I get the same error and have to restart my kernel in Spyder for it to run again. I may need to make a new question for this one. :O – eric Jul 30 '17 at 16:53
  • @RoryDaulton I turned my new problem into a separate question: https://stackoverflow.com/questions/45419033/cannot-render-to-file-in-pygal-more-than-once-in-spyder – eric Jul 31 '17 at 18:15
1

You can also use:

from pygal_maps_world.maps import World

wm = World()

if you open pygal_maps_world plugin physically, you will find a maps.py file and inside that file there is a "World" class. So you can reference that particular file by using module_name.file_name, which in this case is pygal_maps_world.maps and then import that class i.e. "World".

Sahil Gupta
  • 58
  • 1
  • 7
0

Both

from pygal.maps.world import World and from pygal_maps_world.maps import World

are not working in pycharm the program gets executed without any error but there is no image saved in the file it is empty.

Eric Jin
  • 3,836
  • 4
  • 19
  • 45