Could not delete question. Please refer to question: Shade states of a country according to dictionary values with Basemap
I want to plot data (number of sick people for a certain year) on each state of Mexico. I am using jupyter notebook. So far I have seen several options and tutorials, but none seem to seem to explicitly explain how to plot the map of a country. Below I explain some options/tutorial I have seen and why they have not worked (this I do just to argue that tutorials are not very straight forward):
Bokeh (http://docs.bokeh.org/en/latest/docs/gallery/texas.html). In the tutorial texas state is plotted given that us_counties is in bokeh.sampledata. However I have not found other countries in the sampledata.
mpl_toolkits.basemap (http://www.geophysique.be/2011/01/27/matplotlib-basemap-tutorial-07-shapefiles-unleached/). Although I am able to import shapefile, I cannot run
from shapefile import ShapeFile
(ImportError: cannot import name ShapeFile). Furthermore I have not been able to download dbflib library.Vincent (Why Python Vincent map visuzalization does not map data from Data Frame?) When I run the code from the answer in said tutorial no image appears (even though I used command
vincent.core.initialize_notebook()
).Plotly (https://plot.ly/python/choropleth-maps/). The tutorial plots the map of USA importing information from a csv table (no information of other countries available). If wanting to plot another country, would it be possible to make the table?
Explored this 4 options I have found tutorials not to be very clear or easy to follow. I find it hard to believe that plotting a map of a country is difficult in python. I think there must be an easier way than the ones explained in the past tutorials.
The question is: Which is the easiest (hopefully simple) way to plot the map of a certain country (any) with python and how?
I have installed the following packages: matplotlib, pyshp, mpl_toolkits.basemap, bokeh, pandas, numpy. I have also downloaded Mexico's map from http://www.gadm.org/
Thanks in advance.
While this question seems to be unanswerable in its current form, I'll at least note that you seem to be something wrong when using basemap - you don't want to import Shapefile, but simply read it using the
readshapefile
method of aBasemap
object like so:You will then be able to access the coordinates of each state's boundaries via
m.mexican_states
(as a list of arrays) and the corresponding information (such as names, maybe an indentifying code) bym.mexican_states_info
. You will then need some sort of dict or DataFrame containing names/codes for states (corresponding to what is inm.mexican_states_info
) and the values you want to plot. A simple example would work something like this, assuming that you have a dict calledmexican_states_sick_people
that looks something like{"Mexico City":123, "Chiapas":35, ...}
:This example should be more or less functional, if you have a working shapefile of states and make sure that the dataset of sick people you have has some sort of identifier (name or code) for each state that allows you to match up the numbers with the identifier of states in the shapefile (this is what the
shade = ...
line in the loop relies upon - in the example I'm accessing the vals in the dictionary using a name from the shapefile as key).Hope this helps, good luck!