Description: This notebook was made to demonstrate how to make a gif map by merging 2 datasets. The first being a dataset containing mappable coordinates onto which the second dataset may mapping its information of interest.
This lab is split into two sections.
Input(s):
Output: Files, Gif
*please note
This next function was created in previous colabs. We are going to recycle it for use in this lab
CSA2010 | hhchpov15 | hhchpov16 | hhchpov17 | hhchpov18 | hhchpov19 | geometry | |
---|---|---|---|---|---|---|---|
0 | Allendale/Irving... | 38.93 | 34.73 | 32.77 | 35.27 | 32.6 | POLYGON ((-76.65... |
CSA2010 | hhchpov15 | hhchpov16 | hhchpov17 | hhchpov18 | hhchpov19 | geometry | |
---|---|---|---|---|---|---|---|
0 | Allendale/Irving... | 38.93 | 34.73 | 32.77 | 35.27 | 32.6 | POLYGON ((-76.65... |
Fantastic!
Your data is all together in a single dataset.
now what?
First lets take the centerpoint of each geometry. This will be where we place text on the each geometry.
final['centroid'] = final['geometry'].representative_point()Data was successfully merged across all years and geometry.
Now we want the tractname, geometry, and the specific column we want to make a gif from.
# Get only the results tab td = final.copy() td = td.reindex(sorted(td.columns), axis=1)# Coerce columns stored as floats into integers. # This will ensure numbers are rounded to whole digits when displaying the reults gifCols = td.filter(regex=regexMatchingColumnsToMakeTheGifWith).columns.values td[gifCols] = td[gifCols].fillna(-1) td[gifCols] = td[gifCols].astype('int32') td.head()CSA2010 | centroid | geometry | hhchpov15 | hhchpov16 | hhchpov17 | hhchpov18 | hhchpov19 | |
---|---|---|---|---|---|---|---|---|
0 | Allendale/Irving... | POINT (-76.67653... | POLYGON ((-76.65... | 38 | 34 | 32 | 35 | 32 |
1 | Beechfield/Ten H... | POINT (-76.70331... | POLYGON ((-76.69... | 19 | 21 | 23 | 21 | 15 |
2 | Belair-Edison | POINT (-76.57463... | POLYGON ((-76.56... | 36 | 36 | 34 | 39 | 41 |
3 | Brooklyn/Curtis ... | POINT (-76.56060... | POLYGON ((-76.58... | 45 | 46 | 46 | 39 | 41 |
4 | Canton | POINT (-76.58204... | POLYGON ((-76.57... | 5 | 2 | 4 | 4 | 4 |
Data exploration is essential! But not covered in this lab.
td.filter(regex=regexMatchingColumnsToMakeTheGifWith).hist()array([[Everything is almost ready to start making our gifmap!
Lets just get the minimum and maximum values so that our color ramp will have consistent values on each picture.
# Get Min Max mins = [] maxs = [] for col in td.filter(regex=regexMatchingColumnsToMakeTheGifWith).columns: mins.append(td[col].min()) maxs.append(td[col].max()) print(mins, maxs) # set the min and max range for the choropleth map vmin, vmax = min(mins), max(maxs) print('Smallest Value: ', vmin, ', Max Value:', vmax)[3, 0, 0, 0, 0] [65, 65, 64, 60, 66] Smallest Value: 0 , Max Value: 66 merged = td.copy()# For each column for indx, col in enumerate(merged.filter(regex="hhchpov").columns): print('INDEX', indx) print('Col: '+str(col) ) image_name = col+'.jpg' fileNames.append(image_name) # create map, UDPATE: added plt.Normalize to keep the legend range the same for all maps fig = merged.plot(column=col, cmap='Blues', figsize=(10,10), linewidth=0.8, edgecolor='0.8', vmin=vmin, vmax=vmax, legend=True, norm=plt.Normalize(vmin=vmin, vmax=vmax) ) # https://stackoverflow.com/questions/38899190/geopandas-label-polygons if labelBounds: labelColumn = col if specialLabelCol: labelColumn = specialLabelCol merged.apply(lambda x: fig.annotate(s=x[labelColumn], xy=x.geometry.centroid.coords[0], ha='center'),axis=1); # remove axis off chart and set title fig.axis('off') fig.set_title(str(col.replace("hhchpov", "Houshold Childhood Poverty 20")), fontdict={'fontsize': fontsize, 'fontweight' : '3'}) # create an annotation for the data source fig.annotate(annotation, xy=(0.1, .08), xycoords='figure fraction', horizontalalignment='left', verticalalignment='top', fontsize=10, color='#555555') # this will save the figure as a high-res png in the output path. you can also save as svg if you prefer. # filepath = os.path.join(output_path, image_name) chart = fig.get_figure() # fig.savefig(“map_export.png”, dpi=300) chart.savefig(image_name, dpi=300) plt.close(chart) images = [] for filename in fileNames: images.append(imageio.imread(filename)) imageio.mimsave(saveGifAs, images, fps=.5) # This will print out a picture of each picture in the gifmap. from PIL import Image import requests from io import BytesIO for filename in fileNames: img = Image.open(filename) size = 328, 328 img.thumbnail(size, Image.ANTIALIAS) imgINDEX 0 Col: hhchpov15 0 Annotation(-76.6... 1 Annotation(-76.7... 2 Annotation(-76.5... 3 Annotation(-76.5... 4 Annotation(-76.5... 5 Annotation(-76.5... 6 Annotation(-76.6... 7 Annotation(-76.6... 8 Annotation(-76.5... 9 Annotation(-76.5... 10 Annotation(-76.6... 11 Annotation(-76.7... 12 Annotation(-76.6... 13 Annotation(-76.6... 14 Annotation(-76.6... 15 Annotation(-76.5... 16 Annotation(-76.6... 17 Annotation(-76.6... 18 Annotation(-76.6... 19 Annotation(-76.6... 20 Annotation(-76.6... 21 Annotation(-76.6... 22 Annotation(-76.6... 23 Annotation(-76.6... 24 Annotation(-76.5... 25 Annotation(-76.5... 26 Annotation(-76.5... 27 Annotation(-76.5... 28 Annotation(-76.7... 29 Annotation(-76.6... 30 Annotation(-76.5... 31 Annotation(-76.5... 32 Annotation(-76.5... 33 Annotation(-76.6... 34 Annotation(-76.6... 35 Annotation(-76.5... 36 Annotation(-76.6... 37 Annotation(-76.6... 38 Annotation(-76.6... 39 Annotation(-76.5... 40 Annotation(-76.5... 41 Annotation(-76.5... 42 Annotation(-76.5... 43 Annotation(-76.6... 44 Annotation(-76.6... 45 Annotation(-76.6... 46 Annotation(-76.6... 47 Annotation(-76.6... 48 Annotation(-76.5... 49 Annotation(-76.6... 50 Annotation(-76.6... 51 Annotation(-76.6... 52 Annotation(-76.6... 53 Annotation(-76.6... 54 Annotation(-76.6... dtype: object(-76.72049409966101, -76.52058984362787, 39.18850298950285, 39.38074613803655)Text(0.5, 1.0, 'Houshold Childhood Poverty 2015')Text(0.1, 0.08, 'Source: Maryland Vital Statistics; Analysis by: Baltimore Neighborhood Indicators Alliance')INDEX 1 Col: hhchpov16 0 Annotation(-76.6... 1 Annotation(-76.7... 2 Annotation(-76.5... 3 Annotation(-76.5... 4 Annotation(-76.5... 5 Annotation(-76.5... 6 Annotation(-76.6... 7 Annotation(-76.6... 8 Annotation(-76.5... 9 Annotation(-76.5... 10 Annotation(-76.6... 11 Annotation(-76.7... 12 Annotation(-76.6... 13 Annotation(-76.6... 14 Annotation(-76.6... 15 Annotation(-76.5... 16 Annotation(-76.6... 17 Annotation(-76.6... 18 Annotation(-76.6... 19 Annotation(-76.6... 20 Annotation(-76.6... 21 Annotation(-76.6... 22 Annotation(-76.6... 23 Annotation(-76.6... 24 Annotation(-76.5... 25 Annotation(-76.5... 26 Annotation(-76.5... 27 Annotation(-76.5... 28 Annotation(-76.7... 29 Annotation(-76.6... 30 Annotation(-76.5... 31 Annotation(-76.5... 32 Annotation(-76.5... 33 Annotation(-76.6... 34 Annotation(-76.6... 35 Annotation(-76.5... 36 Annotation(-76.6... 37 Annotation(-76.6... 38 Annotation(-76.6... 39 Annotation(-76.5... 40 Annotation(-76.5... 41 Annotation(-76.5... 42 Annotation(-76.5... 43 Annotation(-76.6... 44 Annotation(-76.6... 45 Annotation(-76.6... 46 Annotation(-76.6... 47 Annotation(-76.6... 48 Annotation(-76.5... 49 Annotation(-76.6... 50 Annotation(-76.6... 51 Annotation(-76.6... 52 Annotation(-76.6... 53 Annotation(-76.6... 54 Annotation(-76.6... dtype: object(-76.72049409966101, -76.52058984362787, 39.18850298950285, 39.38074613803655)Text(0.5, 1.0, 'Houshold Childhood Poverty 2016')Text(0.1, 0.08, 'Source: Maryland Vital Statistics; Analysis by: Baltimore Neighborhood Indicators Alliance')INDEX 2 Col: hhchpov17 0 Annotation(-76.6... 1 Annotation(-76.7... 2 Annotation(-76.5... 3 Annotation(-76.5... 4 Annotation(-76.5... 5 Annotation(-76.5... 6 Annotation(-76.6... 7 Annotation(-76.6... 8 Annotation(-76.5... 9 Annotation(-76.5... 10 Annotation(-76.6... 11 Annotation(-76.7... 12 Annotation(-76.6... 13 Annotation(-76.6... 14 Annotation(-76.6... 15 Annotation(-76.5... 16 Annotation(-76.6... 17 Annotation(-76.6... 18 Annotation(-76.6... 19 Annotation(-76.6... 20 Annotation(-76.6... 21 Annotation(-76.6... 22 Annotation(-76.6... 23 Annotation(-76.6... 24 Annotation(-76.5... 25 Annotation(-76.5... 26 Annotation(-76.5... 27 Annotation(-76.5... 28 Annotation(-76.7... 29 Annotation(-76.6... 30 Annotation(-76.5... 31 Annotation(-76.5... 32 Annotation(-76.5... 33 Annotation(-76.6... 34 Annotation(-76.6... 35 Annotation(-76.5... 36 Annotation(-76.6... 37 Annotation(-76.6... 38 Annotation(-76.6... 39 Annotation(-76.5... 40 Annotation(-76.5... 41 Annotation(-76.5... 42 Annotation(-76.5... 43 Annotation(-76.6... 44 Annotation(-76.6... 45 Annotation(-76.6... 46 Annotation(-76.6... 47 Annotation(-76.6... 48 Annotation(-76.5... 49 Annotation(-76.6... 50 Annotation(-76.6... 51 Annotation(-76.6... 52 Annotation(-76.6... 53 Annotation(-76.6... 54 Annotation(-76.6... dtype: object(-76.72049409966101, -76.52058984362787, 39.18850298950285, 39.38074613803655)Text(0.5, 1.0, 'Houshold Childhood Poverty 2017')Text(0.1, 0.08, 'Source: Maryland Vital Statistics; Analysis by: Baltimore Neighborhood Indicators Alliance')INDEX 3 Col: hhchpov18 0 Annotation(-76.6... 1 Annotation(-76.7... 2 Annotation(-76.5... 3 Annotation(-76.5... 4 Annotation(-76.5... 5 Annotation(-76.5... 6 Annotation(-76.6... 7 Annotation(-76.6... 8 Annotation(-76.5... 9 Annotation(-76.5... 10 Annotation(-76.6... 11 Annotation(-76.7... 12 Annotation(-76.6... 13 Annotation(-76.6... 14 Annotation(-76.6... 15 Annotation(-76.5... 16 Annotation(-76.6... 17 Annotation(-76.6... 18 Annotation(-76.6... 19 Annotation(-76.6... 20 Annotation(-76.6... 21 Annotation(-76.6... 22 Annotation(-76.6... 23 Annotation(-76.6... 24 Annotation(-76.5... 25 Annotation(-76.5... 26 Annotation(-76.5... 27 Annotation(-76.5... 28 Annotation(-76.7... 29 Annotation(-76.6... 30 Annotation(-76.5... 31 Annotation(-76.5... 32 Annotation(-76.5... 33 Annotation(-76.6... 34 Annotation(-76.6... 35 Annotation(-76.5... 36 Annotation(-76.6... 37 Annotation(-76.6... 38 Annotation(-76.6... 39 Annotation(-76.5... 40 Annotation(-76.5... 41 Annotation(-76.5... 42 Annotation(-76.5... 43 Annotation(-76.6... 44 Annotation(-76.6... 45 Annotation(-76.6... 46 Annotation(-76.6... 47 Annotation(-76.6... 48 Annotation(-76.5... 49 Annotation(-76.6... 50 Annotation(-76.6... 51 Annotation(-76.6... 52 Annotation(-76.6... 53 Annotation(-76.6... 54 Annotation(-76.6... dtype: object(-76.72049409966101, -76.52058984362787, 39.18850298950285, 39.38074613803655)Text(0.5, 1.0, 'Houshold Childhood Poverty 2018')Text(0.1, 0.08, 'Source: Maryland Vital Statistics; Analysis by: Baltimore Neighborhood Indicators Alliance')INDEX 4 Col: hhchpov19 0 Annotation(-76.6... 1 Annotation(-76.7... 2 Annotation(-76.5... 3 Annotation(-76.5... 4 Annotation(-76.5... 5 Annotation(-76.5... 6 Annotation(-76.6... 7 Annotation(-76.6... 8 Annotation(-76.5... 9 Annotation(-76.5... 10 Annotation(-76.6... 11 Annotation(-76.7... 12 Annotation(-76.6... 13 Annotation(-76.6... 14 Annotation(-76.6... 15 Annotation(-76.5... 16 Annotation(-76.6... 17 Annotation(-76.6... 18 Annotation(-76.6... 19 Annotation(-76.6... 20 Annotation(-76.6... 21 Annotation(-76.6... 22 Annotation(-76.6... 23 Annotation(-76.6... 24 Annotation(-76.5... 25 Annotation(-76.5... 26 Annotation(-76.5... 27 Annotation(-76.5... 28 Annotation(-76.7... 29 Annotation(-76.6... 30 Annotation(-76.5... 31 Annotation(-76.5... 32 Annotation(-76.5... 33 Annotation(-76.6... 34 Annotation(-76.6... 35 Annotation(-76.5... 36 Annotation(-76.6... 37 Annotation(-76.6... 38 Annotation(-76.6... 39 Annotation(-76.5... 40 Annotation(-76.5... 41 Annotation(-76.5... 42 Annotation(-76.5... 43 Annotation(-76.6... 44 Annotation(-76.6... 45 Annotation(-76.6... 46 Annotation(-76.6... 47 Annotation(-76.6... 48 Annotation(-76.5... 49 Annotation(-76.6... 50 Annotation(-76.6... 51 Annotation(-76.6... 52 Annotation(-76.6... 53 Annotation(-76.6... 54 Annotation(-76.6... dtype: object(-76.72049409966101, -76.52058984362787, 39.18850298950285, 39.38074613803655)Text(0.5, 1.0, 'Houshold Childhood Poverty 2019')Text(0.1, 0.08, 'Source: Maryland Vital Statistics; Analysis by: Baltimore Neighborhood Indicators Alliance')