Sunday, December 24, 2017

generate world maps, province-city level

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from geonamescache import GeonamesCache
# from helpers import slug
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from mpl_toolkits.basemap import Basemap

# filename = 'data/API_AG.LND.FRST.ZS_DS2_en_csv_v2.csv'
shapefile = 'shapefile/ne_10m_admin_1_states_provinces'
num_colors = 9
year = '2017'

descripton = '''nwww '''


gc = GeonamesCache()
iso3_codes = list(gc.get_dataset_by_key(gc.get_countries(), 'iso3').keys())


fig = plt.figure()


ax = fig.add_subplot(111, axisbg='w', frame_on=False)
fig.suptitle('world maps {}'.format(year), fontsize=30, y=.95)


m = Basemap(lon_0=0, projection='robin',resolution='l')


m.drawmapboundary(color='w')



m.readshapefile(shapefile, 'units', color='#444444', linewidth=.2)
# m.readshapefile(shapefile, 'units', color='#444444', linewidth=.2)
for info, shape in zip(m.units_info, m.units):
    iso3 = info['name']
  
    color = [0.8089965397923875, 0.92516724336793543, 0.78345251826220685, 1.0]
        # print(scheme[df.ix[iso3]['bin']])
    # print "test"
    patches = [Polygon(np.array(shape), True)]
    pc = PatchCollection(patches)
    pc.set_facecolor(color)
    ax.add_collection(pc)

# Cover up Antarctica so legend can be placed over it.
ax.axhspan(0, 1000 * 1800, facecolor='w', edgecolor='w', zorder=2)

# Draw color legend.
ax_legend = fig.add_axes([0.35, 0.14, 0.3, 0.03], zorder=3)
# cmap = mpl.colors.ListedColormap(scheme)
# cmap = mpl.colors.ListedColormap()
cb = mpl.colorbar.ColorbarBase(ax_legend , orientation='horizontal')

plt.show()




--- shape file source ---
http://www.naturalearthdata.com/downloads/10m-physical-vectors/

No comments:

Post a Comment