Draw polygons more efficiently with matplotlib
You could consider creating Collections of polygons instead of individual polygons.
The relevant docs can be found here: http://matplotlib.org/api/collections_api.htmlWith a example worth picking appart here: http://matplotlib.org/examples/api/collections_demo.html
As an example:
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import PolyCollectionimport matplotlib as mpl# Generate data. In this case, we'll make a bunch of center-points and generate# verticies by subtracting random offsets from those center-pointsnumpoly, numverts = 100, 4centers = 100 * (np.random.random((numpoly,2)) - 0.5)offsets = 10 * (np.random.random((numverts,numpoly,2)) - 0.5)verts = centers + offsetsverts = np.swapaxes(verts, 0, 1)# In your case, "verts" might be something like:# verts = zip(zip(lon1, lat1), zip(lon2, lat2), ...)# If "data" in your case is a numpy array, there are cleaner ways to reorder# things to suit.# Color scalar...# If you have rgb values in your "colorval" array, you could just pass them# in as "facecolors=colorval" when you create the PolyCollectionz = np.random.random(numpoly) * 500fig, ax = plt.subplots()# Make the collection and add it to the plot.coll = PolyCollection(verts, array=z, cmap=mpl.cm.jet, edgecolors='none')ax.add_collection(coll)ax.autoscale_view()# Add a colorbar for the PolyCollectionfig.colorbar(coll, ax=ax)plt.show()
HTH,
I adjusted my code and now it is working flawlessly :)
Here is the working example:
lons = np.array([data['lon1'],data['lon3'],data['lon4'],data['lon2']])lats = np.array([data['lat1'],data['lat3'],data['lat4'],data['lat2']])x,y = m(lons,lats)pols = zip(x,y)pols = np.swapaxes(pols,0,2)pols = np.swapaxes(pols,1,2)coll = PolyCollection(pols,facecolor=colorval,cmap=jet,edgecolor='none',zorder=2)plt.gca().add_collection(coll)