Created
February 22, 2015 08:26
-
-
Save ambivalentno/67b93673b10346d765c9 to your computer and use it in GitHub Desktop.
Plot squares with pyplot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
bbox = { | |
'n': 52.4311573, | |
's': 52.3182742, | |
'e': 5.0683726, | |
'w': 4.7288558, | |
} | |
gh_boxes = [{'s': 52.294921875, 'e': 5.09765625, 'w': 5.0537109375, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 5.0537109375, 'w': 5.009765625, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 5.009765625, 'w': 4.9658203125, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 4.9658203125, 'w': 4.921875, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 4.921875, 'w': 4.8779296875, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 4.8779296875, 'w': 4.833984375, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 4.833984375, 'w': 4.7900390625, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 4.7900390625, 'w': 4.74609375, 'n': 52.3388671875}, {'s': 52.294921875, 'e': 4.74609375, 'w': 4.7021484375, 'n': 52.3388671875}, {'s': 52.3388671875, 'e': 5.09765625, 'w': 5.0537109375, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 5.0537109375, 'w': 5.009765625, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 5.009765625, 'w': 4.9658203125, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 4.9658203125, 'w': 4.921875, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 4.921875, 'w': 4.8779296875, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 4.8779296875, 'w': 4.833984375, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 4.833984375, 'w': 4.7900390625, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 4.7900390625, 'w': 4.74609375, 'n': 52.3828125}, {'s': 52.3388671875, 'e': 4.74609375, 'w': 4.7021484375, 'n': 52.3828125}, {'s': 52.3828125, 'e': 5.09765625, 'w': 5.0537109375, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 5.0537109375, 'w': 5.009765625, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 5.009765625, 'w': 4.9658203125, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 4.9658203125, 'w': 4.921875, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 4.921875, 'w': 4.8779296875, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 4.8779296875, 'w': 4.833984375, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 4.833984375, 'w': 4.7900390625, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 4.7900390625, 'w': 4.74609375, 'n': 52.4267578125}, {'s': 52.3828125, 'e': 4.74609375, 'w': 4.7021484375, 'n': 52.4267578125}, {'s': 52.4267578125, 'e': 5.09765625, 'w': 5.0537109375, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 5.0537109375, 'w': 5.009765625, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 5.009765625, 'w': 4.9658203125, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 4.9658203125, 'w': 4.921875, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 4.921875, 'w': 4.8779296875, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 4.8779296875, 'w': 4.833984375, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 4.833984375, 'w': 4.7900390625, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 4.7900390625, 'w': 4.74609375, 'n': 52.470703125}, {'s': 52.4267578125, 'e': 4.74609375, 'w': 4.7021484375, 'n': 52.470703125}] | |
def bbox_to_rectangle(bbox, color='g', transparency=None, z=0, fill=False): | |
''' | |
As matplotlib.patches.Rectangle draws a rectangle with | |
lower left at xy = (x, y) with specified width and height, | |
We have to specify lower left and width/height. | |
''' | |
# We have Norht-South as y and West-East as x | |
south_west = (bbox['w'], bbox['s']) | |
width = bbox['e'] - bbox['w'] | |
height = bbox['n'] - bbox['s'] | |
return plt.Rectangle( | |
south_west, width, height, | |
fc=color, | |
alpha=transparency, | |
zorder=z, | |
fill=fill | |
) | |
plt.axes() | |
# Put basic rectangle (area to fill). | |
rectangle = bbox_to_rectangle(bbox, fill=True) | |
plt.gca().add_patch(rectangle) | |
# Put those rectangles we fill with. | |
for box in gh_boxes: | |
rect = bbox_to_rectangle(box, z=1, color='r', fill=True, transparency=0.5) | |
plt.gca().add_patch(rect) | |
plt.axis('scaled') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bbox is a randomly selected area on the surface of the earth (small enough to treat earth as flat one)
gh_boxes are generated by my script (based on https://code.google.com/p/python-geohash/wiki/GeohashReference and https://github.com/davidmoten/geo)