Monday, December 11, 2017

generation world map


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 = 'countries/ne_10m_admin_0_countries'
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(figsize=(22, 12))

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')
m.drawmapboundary(color='w')

m.readshapefile(shapefile, 'units', color='#444444', linewidth=.2)
for info, shape in zip(m.units_info, m.units):
    iso3 = info['ADM0_A3']
 
    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)

cb = mpl.colorbar.ColorbarBase(ax_legend , orientation='horizontal')

plt.annotate(descripton, xy=(-.8, -3.2), size=14, xycoords='axes fraction')

# plt.savefig("worldmap.png", bbox_inches='tight', pad_inches=.2)
plt.show()




-usage

./generateworldmap

No comments:

Post a Comment