Skip to content

Instantly share code, notes, and snippets.

@jimrybarski
Last active January 2, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimrybarski/8314881 to your computer and use it in GitHub Desktop.
Save jimrybarski/8314881 to your computer and use it in GitHub Desktop.
One of the worst pieces of code ever
""" I did not write this, though I fixed a syntax error in it. I just found it while looking for an easy way to plot a world choropleth map. It is probably the worst program I've ever seen and felt I should keep it for some reason. """
import shapefile
import matplotlib.cm as jimbo
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import numpy as np
# -- input --
sf = shapefile.Reader("ne_110m_admin_0_countries.shp")
recs = sf.records()
shapes = sf.shapes()
Nshp = len(shapes)
cns = []
for nshp in xrange(Nshp):
cns.append(recs[nshp][1])
cns = np.array(cns)
cm = jimbo.get_cmap('Dark2')
cccol = cm(1.*np.arange(Nshp)/Nshp)
# -- plot --
fig = plt.figure()
ax = fig.add_subplot(111)
for nshp in xrange(Nshp):
ptchs = []
pts = np.array(shapes[nshp].points)
prt = shapes[nshp].parts
par = list(prt) + [pts.shape[0]]
for pij in xrange(len(prt)):
ptchs.append(Polygon(pts[par[pij]:par[pij+1]]))
ax.add_collection(PatchCollection(ptchs,facecolor=cccol[nshp,:],edgecolor='k', linewidths=.1))
ax.set_xlim(-180,+180)
ax.set_ylim(-90,90)
#fig.savefig('test.png')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment