Professional Documents
Culture Documents
Showalloutput
Hidealloutput
Visualization:MappingGlobalEarthquakeActivity
ThisprojectintroducestheBasemaplibrary,whichcanbeusedtocreatemapsandplotgeographicaldatasets.
Home(./)
Contents
Introduction
Inlineoutput
InstallingmatplotlibandBasemap
UsingMinicondatoinstallmatplotlibandBasemap
Createacondaenvironment
Rememberingtoactivatetheenvironment
Makingasimplemap
Addingdetail
Zoomingin
Plottingpointsonasimplemap
Labelingpoints
Aglobalearthquakedataset
Parsingthedata
Plottingearthquakes
Addingcolor
Addingatitle
Otherinterestingdatasetstoexplore
Conclusion
Introduction
Themaingoalofthisprojectistohelpyougetcomfortablemakingmapsofgeographicaldata.Ifyoufollowalongwiththetutorial,you'llendup
makingthismap:
http://introtopython.org/visualization_earthquakes.html#
1/19
12/28/2015
Ittakesfewerthan50linesofcodetogeneratethismapfromarawdataset!Thisprojectwillconcludewithalistofdatasetstoexplore,tohelpyou
findaprojectofyourowntotry.
top
Inlineoutput
Thefollowingcodehelpsmakeallofthecodesamplesinthisnotebookdisplaytheiroutputproperly.Ifyou'rerunningtheseprogramsas
standalonePythonprograms,youdon'tneedtoworryaboutthiscode.
Ifyou'reusingIPythonNotebookforthiswork,youneedthiscellinyournotebook.Alsonotethatyouneedtorunthiscellbeforerunninganyother
cellinthenotebook.Otherwiseyouroutputwilldisplayinaseparatewindow,oritwon'tdisplayatall.Ifyoutrytorunacellandtheoutputdoesnot
displayinthenotebook:
RestarttheIPythonNotebookkernel.
Runthefollowingcell.
Runthecellyouwereinterestedinagain.
#Thisjustletstheoutputofthefollowingcodesamples
#displayinlineonthispage,atanappropriatesize.
frompylabimportrcParams
%matplotlibinline
rcParams['figure.figsize']=(8,6)
InstallingmatplotlibandBasemap
TheseinstructionsarewrittenforUbuntufirst,andinstructionsspecifictootheroperatingsystemswillbeaddedshortly.
Pythonsmatplotlib(http://matplotlib.org/)packageisanamazingresource,andtheBasemaptoolkit(http://matplotlib.org/basemap/)extends
matplotlibscapabilitiestomappingapplications.InstallingthesepackagesisstraightforwardusingUbuntuspackagemanagementsystem,but
thesepackagesmaynotworkforallmapsyouwanttoproduce.Inthatcase,itisgoodtoknowhowtoinstalleachpackagefromsource.
UsingMinicondatoinstallmatplotlibandBasemap
Conda(http://conda.pydata.org/index.html)isapackagemanagerputoutbythepeopleatContinuumAnalytics(http://continuum.io/).Theconda
packageinstallermakesitquitesimpletoinstallmatplotlibandbasemap.Beawarethatifyou'reusuingvirtualenvatall,condacanconflictwiththe
wayvirtualenvworks.Butifyouwanttogetintomapping,it'sprobablyworthwhiletolearnaboutconda.
ContinuumAnalyticsputsoutadistributionofPythoncalledAnaconda,whichincludesalargesetofpackagesthatsupportdataprocessingand
scientificcomputing.We'llinstallMiniconda,whichjustinstallsthecondapackagemanagerwe'llthenusecondatoinstallthematplotliband
Basemappackages.
ToinstallMiniconda,gototheMinicondahomepage(http://conda.pydata.org/miniconda.html).Downloadandruntheappropriateinstallerforyour
system.Thefollowingcommandswillgetcondasetupona32bitLinuxsystem:
~$wgethttp://repo.continuum.io/miniconda/Miniconda3latestLinuxx86.sh
~$bashMiniconda3latestLinuxx86.sh
~$#SayyestothepromptforprependingMiniconda3installlocationtoPATH
~$execbash
http://introtopython.org/visualization_earthquakes.html#
2/19
12/28/2015
Createacondaenvironment
Onceyou'vegotcondainstalled,you'lluseittomakeanenvironmentforthisproject.Makeadirectoryforthisproject,andstartaconda
environmentthatincludesmatplotlibandbasemap:
$mkdirvisualization_eq&&cdvisualization_eq
visualization_eq$condacreateneq_envpython=3matplotlibbasemappillow
visualization_eq$sourceactivateeq_env
(eq_env)visualization_eq$
Thecommand condacreateneq_envpython=3matplotlibbasemappillowdoestwothings:
Itcreatesanenvironmentspecifictothisproject,called eq_env,whichusesPython3.condawillmakesureanypackagesinstalledfor
thisprojectdon'tinterferewithpackagesinstalledforanotherprojectthatcondamanages.
Itcheckstoseeifthepackageswe'velisted, matplotlib, basemap,and pillowhavealreadybeeninstalledbyconda.Iftheyhave,
condawillmakethemavailableinthisenvironment.Iftheyhaven'tbeeninstalled,condawilldownloadandinstallthepackages,andmake
themavailableinthisenvironment.( pillowisanimageprocessinglibrary)
Rememberingtoactivatetheenvironment
Anytimeyou'regoingtoworkonthisproject,makesureyouactivatetheenvironmentwiththecommand
visualization_eq$sourceactivateeq_env
(eq_env)visualization_eq$
Thismakesthepackagesassociatedwiththeenvironmentavailabletoanyprogramyoustoreinthedirectory visualization_eq.
top
Makingasimplemap
Let'sstartoutbymakingasimplemapoftheworld.Ifyourunthefollowingcode,youshouldgetanicemapoftheglobe,withgoodclean
coastlines:
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='ortho',lat_0=50,lon_0=100,
resolution='l',area_thresh=1000.0)
my_map.drawcoastlines()
plt.show()
showoutput
top
Addingdetail
Letsaddsomemoredetailtothismap,startingwithcountryborders.Addthefollowinglinesafter map.drawcoastlines():
http://introtopython.org/visualization_earthquakes.html#
3/19
12/28/2015
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='ortho',lat_0=50,lon_0=100,
resolution='l',area_thresh=1000.0)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
plt.show()
showoutput
Youshouldseethecontinentsfilledin.Nowletscleanuptheedgeoftheglobe:
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='ortho',lat_0=50,lon_0=100,
resolution='l',area_thresh=1000.0)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
plt.show()
showoutput
Youshouldseeacleanercircleoutliningtheglobe.Nowletsdrawlatitudeandlongitudelines:
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='ortho',lat_0=50,lon_0=100,
resolution='l',area_thresh=1000.0)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
my_map.drawmeridians(np.arange(0,360,30))
my_map.drawparallels(np.arange(90,90,30))
plt.show()
showoutput
The np.arange()argumentstellwhereyourlatitudeandlongitudelinesshouldbeginandend,andhowfaraparttheyshouldbespaced.
Letsplaywithtwoofthemapsettings,andthenwe'llmoveontoplottingdataonthisglobe.Letsstartbyadjustingtheperspective.Changethe
latitudeandlongitudeparametersintheoriginalBasemapdefinitionto0and100.Whenyouruntheprogram,youshouldseeyourmapcentered
alongtheequator:
http://introtopython.org/visualization_earthquakes.html#
4/19
12/28/2015
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='ortho',lat_0=0,lon_0=100,
resolution='l',area_thresh=1000.0)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
my_map.drawmeridians(np.arange(0,360,30))
my_map.drawparallels(np.arange(90,90,30))
plt.show()
showoutput
Nowletschangethekindofmapwe'reproducing.Changetheprojectiontypetorobin.YoushouldendupwithaRobinsonprojectioninsteadofa
globe:
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='robin',lat_0=0,lon_0=100,
resolution='l',area_thresh=1000.0)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
my_map.drawmeridians(np.arange(0,360,30))
my_map.drawparallels(np.arange(90,90,30))
plt.show()
showoutput
top
Zoomingin
Beforewemoveontoplottingpointsonthemap,letsseehowtozoominonaregion.Thisisgoodtoknowbecausetherearemanydatasets
specifictooneregionoftheworld,whichwouldgetlostwhenplottedonamapofthewholeworld.Someprojectionscannotbezoomedinatall,so
ifthingsarenotworkingwell,makesuretolookatthedocumentation(http://matplotlib.org/basemap/api/basemap_api.html).
IliveonBaranofIslandinsoutheastAlaska,soletszoominonthatregion.Onewaytozooministospecifythelatitudeandlongitudeofthelower
leftandupperrightcornersoftheregionyouwanttoshow.Letsuseamercatorprojection,whichsupportsthismethodofzooming.Thenotation
forlowerleftcornerat136.25degreeswestand56degreesnorthis:
llcrnrlon=136.25,llcrnrlat=56.0
So,thefullsetofparameterswe'lltryis:
http://introtopython.org/visualization_earthquakes.html#
5/19
12/28/2015
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='l',area_thresh=1000.0,
llcrnrlon=136.25,llcrnrlat=56,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
my_map.drawmeridians(np.arange(0,360,30))
my_map.drawparallels(np.arange(90,90,30))
plt.show()
showoutput
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='h',area_thresh=1000.0,
llcrnrlon=136.25,llcrnrlat=56,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
my_map.drawmeridians(np.arange(0,360,30))
my_map.drawparallels(np.arange(90,90,30))
plt.show()
hideoutput
Thisismuchbetter,butwe'restillmissinganentireislandtothewest.Thisisbecauseofthe area_threshsetting.Thissettingspecifieshow
largeafeaturemustbeinordertoappearonthemap.Thecurrentsettingwillonlyshowfeatureslargerthan1000squarekilometers.Thisisa
reasonablesettingforalowresolutionmapoftheworld,butit'sareallybadchoiceforasmallscalemap.Letschangethatsettingto0.1,andsee
howmuchdetailweget:
http://introtopython.org/visualization_earthquakes.html#
6/19
12/28/2015
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
#makesurethevalueofresolutionisalowercaseL,
#for'low',notanumeral1
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='h',area_thresh=0.1,
llcrnrlon=136.25,llcrnrlat=56,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
my_map.drawmeridians(np.arange(0,360,30))
my_map.drawparallels(np.arange(90,90,30))
plt.show()
showoutput
Thisisnowameaningfulmap.WecanseeKruzofisland,thelargeislandtothewestofBaranof,andmanyotherislandsinthearea.Settingslower
than area_thresh=0.1won'taddanynewdetailsatthislevelofzoom.
Basemapisanincrediblyflexiblepackage.Ifyou'recurioustoplayaroundwithothersettings,takealookattheBasemapdocumentation
(http://matplotlib.org/basemap/api/basemap_api.html).Next,we'lllearnhowtoplotpointsonourmaps.
top
Plottingpointsonasimplemap
It'satestamenttothehardworkofmanyotherpeoplethatwecancreateamapliketheoneaboveinlessthan15linesofcode!Nowletsadd
somepointstothemap.IliveinSitka,thelargestcommunityonBaranofIsland,soletsaddapointshowingSitkaslocation.Addthefollowinglines
justbeforeplt.show():
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='h',area_thresh=0.1,
llcrnrlon=136.25,llcrnrlat=56.0,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
lon=135.3318
lat=57.0799
x,y=my_map(lon,lat)
my_map.plot(x,y,'bo',markersize=12)
plt.show()
showoutput
Theonlynonobviouslinehereisthe boargument,whichtellsbasemaptouseabluecircleforthepoint.Therearequiteanumberofcolorsand
symbolsyoucanuse.Formorechoices,seethedocumentationforthematplotlib.pyplot.plot
(http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.plot)function.Thedefaultmarkersizeis6,butthatwastoosmallonthisparticular
map.Amarkersizeof12showsupnicelyonthismap.
Plottingasinglepointisnice,butweoftenwanttoplotalargesetofpointsonamap.TherearetwoothercommunitiesonBaranofIsland,solets
showwherethosetwocommunitiesareonthismap.Westorethelatitudesandlongitudesofourpointsintwoseparatelists,mapthosetoxandy
coordinates,andplotthosepointsonthemap.Withmoredotsonthemap,wealsowanttoreducethemarkersizeslightly:
http://introtopython.org/visualization_earthquakes.html#
7/19
12/28/2015
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='h',area_thresh=0.1,
llcrnrlon=136.25,llcrnrlat=56.0,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
lons=[135.3318,134.8331,134.6572]
lats=[57.0799,57.0894,56.2399]
x,y=my_map(lons,lats)
my_map.plot(x,y,'bo',markersize=10)
plt.show()
showoutput
top
Labelingpoints
Nowletslabelthesethreepoints.Wemakealistofourlabels,andloopthroughthatlist.Weneedtoincludethexandyvaluesforeachpointin
thisloop,soBasemapcanfigureoutwheretoplaceeachlabel.
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='h',area_thresh=0.1,
llcrnrlon=136.25,llcrnrlat=56.0,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
lons=[135.3318,134.8331,134.6572]
lats=[57.0799,57.0894,56.2399]
x,y=my_map(lons,lats)
my_map.plot(x,y,'bo',markersize=10)
labels=['Sitka','BaranofWarmSprings','PortAlexander']
forlabel,xpt,yptinzip(labels,x,y):
plt.text(xpt,ypt,label)
plt.show()
showoutput
Ourtownsarenowlabeled,butthelabelsstartrightontopofthepoints.Wecanaddoffsetstothesepoints,sotheyaren'trightontopofthepoints.
Letsmoveallofthelabelsalittleupandtotheright.(Ifyou'recurious,theseoffsetsareinmapprojectioncoordinates
(http://matplotlib.org/basemap/users/mapcoords.html),whicharemeasuredinmeters.Thismeansourcodeactuallyplacesthelabels10kmtothe
eastand5kmtothenorthoftheactualtownsites.)
http://introtopython.org/visualization_earthquakes.html#
8/19
12/28/2015
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='h',area_thresh=0.1,
llcrnrlon=136.25,llcrnrlat=56.0,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
lons=[135.3318,134.8331,134.6572]
lats=[57.0799,57.0894,56.2399]
x,y=my_map(lons,lats)
my_map.plot(x,y,'bo',markersize=10)
labels=['Sitka','BaranofWarmSprings','PortAlexander']
forlabel,xpt,yptinzip(labels,x,y):
plt.text(xpt+10000,ypt+5000,label)
plt.show()
showoutput
Thisisbetter,butonamapofthisscalethesameoffsetdoesn'tworkwellforallpoints.Wecouldploteachlabelindividually,butit'seasiertomake
twoliststostoreouroffsets:
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
my_map=Basemap(projection='merc',lat_0=57,lon_0=135,
resolution='h',area_thresh=0.1,
llcrnrlon=136.25,llcrnrlat=56.0,
urcrnrlon=134.25,urcrnrlat=57.75)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
lons=[135.3318,134.8331,134.6572]
lats=[57.0799,57.0894,56.2399]
x,y=my_map(lons,lats)
my_map.plot(x,y,'bo',markersize=10)
labels=['Sitka','Baranof\nWarmSprings','PortAlexander']
x_offsets=[10000,20000,25000]
y_offsets=[5000,50000,35000]
forlabel,xpt,ypt,x_offset,y_offsetinzip(labels,x,y,x_offsets,y_offsets):
plt.text(xpt+x_offset,ypt+y_offset,label)
plt.show()
showoutput
There'snoeasywaytokeepBaranofWarmSpringsfromcrossingaborder,buttheuseofnewlineswithinalabelmakesitalittlemorelegible.
Nowthatweknowhowtoaddpointstoamap,wecanmoveontolargerdatasets.
top
http://introtopython.org/visualization_earthquakes.html#
9/19
12/28/2015
Aglobalearthquakedataset
TheUSgovernmentmaintainsasetoflivefeeds(http://earthquake.usgs.gov/earthquakes/feed/v1.0/)ofearthquakerelateddatafromrecent
seismicevents.Youcanchoosetoexaminedatafromthelasthour,throughthelastthirtydays.Youcanchoosetoexaminedatafromeventsthat
haveavarietyofmagnitudes.Forthisproject,we'lluseadatasetthatcontainsallseismiceventsoverthelastsevendays,whichhaveamagnitude
of1.0orgreater.
Youcanalsochoosefromavarietyofformats.Inthisfirstexample,we'lllookathowtoparseafileinthecsvformat(commaseparatedvalue).
Therearemoreconvenientformatstoworkwithsuchasjson,butnotalldatasetsareneatlyorganized.We'llstartoutparsingacsvfile,andthen
perhapstakealookathowtoworkwiththejsonformat.
Tofollowthisprojectonyourownsystem,gototheUSGSsource(http://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php)forcsvfilesof
earthquakedataanddownloadthefile"M1.0+Earthquakes"underthe"Past7Days"header.Ifyoulike,hereisadirectlink
(http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_week.csv)tothatfile.Thisdataisupdatedevery5minutes,soyourdatawon't
matchwhatyouseehereexactly.Theformatshouldmatch,butthedataitselfwon'tmatch.
Parsingthedata
Ifweexaminethefirstfewlinesofthetextfileofthedataset,wecanidentifytheinformationthat'smostrelevanttous:
time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type
20150425T14:25:06.520Z,40.6044998,121.8546677,17.65,1.92,md,7,172,0.0806,0.02,nc,nc72435025,20150
425T14:31:51.640Z,"12kmNNEofShingletown,California",earthquake
20150425T14:21:16.420Z,37.6588326,122.5056686,7.39,2.02,md,21,99,0.07232,0.04,nc,nc72435020,20150
425T14:26:05.693Z,"3kmSSWofBroadmoor,California",earthquake
20150425T14:14:40.000Z,62.6036,147.6845,43.1,1.7,ml,,,,0.67,ak,ak11565061,20150425T14:40:18.782
Z,"108kmNNEofSuttonAlpine,Alaska",earthquake
20150425T14:10:02.830Z,27.5843,85.6622,10,4.6,mb,,86,5.898,0.87,us,us20002965,20150425T14:38:25.1
74Z,"14kmEofPanaoti,Nepal",earthquake
20150425T13:55:47.040Z,33.0888333,116.0531667,5.354,1.09,ml,23,38,0.1954,0.26,ci,ci37151511,20150
425T13:59:57.204Z,"10kmSEofOcotilloWells,California",earthquake
Fornow,we'reonlyinterestedinthelatitudeandlongitudeofeachearthquake.Ifwelookatthefirstline,itlookslikewe'reinterestedinthesecond
andthirdcolumnsofeachline.Inthedirectorywhereyousaveyourprogramfiles,makeadirectorycalleddatasets.Savethetextfileas
earthquake_data.csvinthisnewdirectory.
top
UsingPython'scsvmoduletoparsethedata
We'llprocessthedatausingPython'scsvmodule(http://docs.python.org/3.3/library/csv.html)module,whichsimplifiestheprocessofworkingwith
csvfiles.
Thefollowingcodeproducestwolists,containingthelatitudesandlongitudesofeachearthquakeinthefile:
http://introtopython.org/visualization_earthquakes.html#
10/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthelatitudesandlongitudes.
lats,lons=[],[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
#Displaythefirst5latsandlons.
print('lats',lats[0:5])
print('lons',lons[0:5])
showoutput
Wecreateemptyliststocontainthelatitudesandlongitudes.Thenweusethe withstatementtoensurethatthefileclosesproperlyonceithas
beenread,evenifthereareerrorsinprocessingthefile.
Withthedatafileopen,weinitializeacsvreaderobject.The next()functionskipsovertheheaderrow.Thenweloopthrougheachrowinthe
datafile,andpullouttheinformationwewant.
top
Plottingearthquakes
Usingwhatwelearnedaboutplottingasetofpoints,wecannowmakeasimpleplotofthesepoints:
http://introtopython.org/visualization_earthquakes.html#
11/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthelatitudesandlongitudes.
lats,lons=[],[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
#BuildMap
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
eq_map=Basemap(projection='robin',resolution='l',area_thresh=1000.0,
lat_0=0,lon_0=130)
eq_map.drawcoastlines()
eq_map.drawcountries()
eq_map.fillcontinents(color='gray')
eq_map.drawmapboundary()
eq_map.drawmeridians(np.arange(0,360,30))
eq_map.drawparallels(np.arange(90,90,30))
x,y=eq_map(lons,lats)
eq_map.plot(x,y,'ro',markersize=6)
plt.show()
showoutput
Thisisprettycoolinabout40linesofcodewe'veturnedagianttextfileintoaninformativemap.Butthere'sonefairlyobviousimprovementwe
shouldmakeletstrytomakethepointsonthemaprepresentthemagnitudeofeachearthquake.Westartoutbyreadingthemagnitudesintoa
listalongwiththelatitudesandlongitudesofeachearthquake:
http://introtopython.org/visualization_earthquakes.html#
12/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthedataweareinterestedin.
lats,lons=[],[]
magnitudes=[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
magnitudes.append(float(row[4]))
#BuildMap
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
eq_map=Basemap(projection='robin',resolution='l',area_thresh=1000.0,
lat_0=0,lon_0=130)
eq_map.drawcoastlines()
eq_map.drawcountries()
eq_map.fillcontinents(color='gray')
eq_map.drawmapboundary()
eq_map.drawmeridians(np.arange(0,360,30))
eq_map.drawparallels(np.arange(90,90,30))
x,y=eq_map(lons,lats)
eq_map.plot(x,y,'ro',markersize=6)
plt.show()
showoutput
Nowinsteadofplottingallthepointsatonce,we'llloopthroughthepointsandplotthemoneatatime.Whenweploteachpoint,we'lladjustthedot
sizeaccordingtothemagnitude.Sincethemagnitudesstartat1.0,wecansimplyusethemagnitudeasascalefactor.Togetthemarkersize,we
justmultiplythemagnitudebythesmallestdotwewantonourmap:
http://introtopython.org/visualization_earthquakes.html#
13/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthedataweareinterestedin.
lats,lons=[],[]
magnitudes=[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
magnitudes.append(float(row[4]))
#BuildMap
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
eq_map=Basemap(projection='robin',resolution='l',area_thresh=1000.0,
lat_0=0,lon_0=130)
eq_map.drawcoastlines()
eq_map.drawcountries()
eq_map.fillcontinents(color='gray')
eq_map.drawmapboundary()
eq_map.drawmeridians(np.arange(0,360,30))
eq_map.drawparallels(np.arange(90,90,30))
min_marker_size=2.5
forlon,lat,maginzip(lons,lats,magnitudes):
x,y=eq_map(lon,lat)
msize=mag*min_marker_size
eq_map.plot(x,y,'ro',markersize=msize)
plt.show()
showoutput
Wedefineourminimummarkersizeandthenloopthroughallthedatapoints,calculatingthemarkersizeforeachpointbymultiplyingthe
magnitudeoftheearthquakebytheminimummarkersize.
Ifyouhaven'tusedthe zip()functionbefore,ittakesanumberoflists,andpullsoneitemfromeachlist.Oneachloopiteration,wehavea
matchingsetoflongitude,latitude,andmagnitudeofeachearthquake.
top
Addingcolor
There'sonemorechangewecanmake,togenerateamoremeaningfulvisualization.Letsusesomedifferentcolorstorepresentthemagnitudes
aswell.Let'smakesmallearthquakesgreen,moderateearthquakesyellow,andsignificantearthquakesred.Thefollowingversionincludesa
functionthatidentifiestheappropriatecolorforeachearthquake:
http://introtopython.org/visualization_earthquakes.html#
14/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthedataweareinterestedin.
lats,lons=[],[]
magnitudes=[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
magnitudes.append(float(row[4]))
#BuildMap
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
defget_marker_color(magnitude):
#Returnsgreenforsmallearthquakes,yellowformoderate
#earthquakes,andredforsignificantearthquakes.
ifmagnitude<3.0:
return('go')
elifmagnitude<5.0:
return('yo')
else:
return('ro')
eq_map=Basemap(projection='robin',resolution='l',area_thresh=1000.0,
lat_0=0,lon_0=130)
eq_map.drawcoastlines()
eq_map.drawcountries()
eq_map.fillcontinents(color='gray')
eq_map.drawmapboundary()
eq_map.drawmeridians(np.arange(0,360,30))
eq_map.drawparallels(np.arange(90,90,30))
min_marker_size=2.5
forlon,lat,maginzip(lons,lats,magnitudes):
x,y=eq_map(lon,lat)
msize=mag*min_marker_size
marker_string=get_marker_color(mag)
eq_map.plot(x,y,marker_string,markersize=msize)
plt.show()
showoutput
Nowwecaneasilyseewherethemostsignificantearthquakesarehappening.
top
Addingatitle
Beforewefinish,letsaddatitletoourmap.Ourtitleneedstoincludethedaterangefortheseearthquakes,whichrequiresustopullinalittlemore
datawhenweparsetherawtext.Tomakethetitle,we'llusethedatesofthefirstandlastearthquakes.Sincethefileincludesthemostrecent
earthquakesfirst,weneedtousethelastitemsasthestartingdate:
http://introtopython.org/visualization_earthquakes.html#
15/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthedataweareinterestedin.
lats,lons=[],[]
magnitudes=[]
timestrings=[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
magnitudes.append(float(row[4]))
timestrings.append(row[0])
#BuildMap
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
defget_marker_color(magnitude):
#Returnsgreenforsmallearthquakes,yellowformoderate
#earthquakes,andredforsignificantearthquakes.
ifmagnitude<3.0:
return('go')
elifmagnitude<5.0:
return('yo')
else:
return('ro')
eq_map=Basemap(projection='robin',resolution='l',area_thresh=1000.0,
lat_0=0,lon_0=130)
eq_map.drawcoastlines()
eq_map.drawcountries()
eq_map.fillcontinents(color='gray')
eq_map.drawmapboundary()
eq_map.drawmeridians(np.arange(0,360,30))
eq_map.drawparallels(np.arange(90,90,30))
min_marker_size=2.5
forlon,lat,maginzip(lons,lats,magnitudes):
x,y=eq_map(lon,lat)
msize=mag*min_marker_size
marker_string=get_marker_color(mag)
eq_map.plot(x,y,marker_string,markersize=msize)
title_string="EarthquakesofMagnitude1.0orGreater\n"
title_string+="%sthrough%s"%(timestrings[1],timestrings[0])
plt.title(title_string)
plt.show()
showoutput
Thisisgood,butthetimezoneformatmakesthetitlealittlehardertoread.Let'sjustusethedates,andignorethetimesinourtitle.Wecandothis
bykeepingthefirst10charactersofeach timestring,usingaslice: timestring[:10].Sincethisisthefinaliterationforthisproject,let's
alsomaketheplotsizelargeraswell:
http://introtopython.org/visualization_earthquakes.html#
16/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthedataweareinterestedin.
lats,lons=[],[]
magnitudes=[]
timestrings=[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
magnitudes.append(float(row[4]))
timestrings.append(row[0])
#BuildMap
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
defget_marker_color(magnitude):
#Returnsgreenforsmallearthquakes,yellowformoderate
#earthquakes,andredforsignificantearthquakes.
ifmagnitude<3.0:
return('go')
elifmagnitude<5.0:
return('yo')
else:
return('ro')
#Makethisplotlarger.
plt.figure(figsize=(16,12))
eq_map=Basemap(projection='robin',resolution='l',area_thresh=1000.0,
lat_0=0,lon_0=130)
eq_map.drawcoastlines()
eq_map.drawcountries()
eq_map.fillcontinents(color='gray')
eq_map.drawmapboundary()
eq_map.drawmeridians(np.arange(0,360,30))
eq_map.drawparallels(np.arange(90,90,30))
min_marker_size=2.5
forlon,lat,maginzip(lons,lats,magnitudes):
x,y=eq_map(lon,lat)
msize=mag*min_marker_size
marker_string=get_marker_color(mag)
eq_map.plot(x,y,marker_string,markersize=msize)
title_string="EarthquakesofMagnitude1.0orGreater\n"
title_string+="%sthrough%s"%(timestrings[1][:10],timestrings[0][:10])
plt.title(title_string)
plt.show()
showoutput
Togetasenseofhowflexiblethebasemaplibraryis,checkouthowquicklyyoucanchangethelookandfeelofthismap.Commentouttheline
thatcolorsthecontinents,andreplaceitwithacalltobluemarble.Youmightwanttoadjust min_marker_sizeaswell,ifyoumakethismapon
yourownsystem:
http://introtopython.org/visualization_earthquakes.html#
17/19
12/28/2015
importcsv
#Opentheearthquakedatafile.
filename='datasets/earthquake_data.csv'
#Createemptylistsforthedataweareinterestedin.
lats,lons=[],[]
magnitudes=[]
timestrings=[]
#Readthroughtheentirefile,skipthefirstline,
#andpulloutjustthelatsandlons.
withopen(filename)asf:
#Createacsvreaderobject.
reader=csv.reader(f)
#Ignoretheheaderrow.
next(reader)
#Storethelatitudesandlongitudesintheappropriatelists.
forrowinreader:
lats.append(float(row[1]))
lons.append(float(row[2]))
magnitudes.append(float(row[4]))
timestrings.append(row[0])
#BuildMap
frommpl_toolkits.basemapimportBasemap
importmatplotlib.pyplotasplt
importnumpyasnp
defget_marker_color(magnitude):
#Returnsgreenforsmallearthquakes,yellowformoderate
#earthquakes,andredforsignificantearthquakes.
ifmagnitude<3.0:
return('go')
elifmagnitude<5.0:
return('yo')
else:
return('ro')
eq_map=Basemap(projection='robin',resolution='l',area_thresh=1000.0,
lat_0=0,lon_0=130)
eq_map.drawcoastlines()
eq_map.drawcountries()
#eq_map.fillcontinents(color='gray')
eq_map.bluemarble()
eq_map.drawmapboundary()
eq_map.drawmeridians(np.arange(0,360,30))
eq_map.drawparallels(np.arange(90,90,30))
min_marker_size=2.25
forlon,lat,maginzip(lons,lats,magnitudes):
x,y=eq_map(lon,lat)
msize=mag*min_marker_size
marker_string=get_marker_color(mag)
eq_map.plot(x,y,marker_string,markersize=msize)
title_string="EarthquakesofMagnitude1.0orGreater\n"
title_string+="%sthrough%s"%(timestrings[1][:10],timestrings[0][:10])
plt.title(title_string)
plt.show()
showoutput
Wecouldcontinuetorefinethismap.Forexample,wecouldreadthedatafromtheurlinsteadoffromadownloadedtextfile.Thatwaythemap
wouldalwaysbecurrent.Wecoulddetermineeachdotscoloronacontinuousscaleofgreens,yellows,andreds.Nowthatyouhaveabetter
senseofhowtoworkwithbasemap,Ihopeyouenjoyplayingwiththesefurtherrefinementsasyoumapthedatasetsyou'remostinterestedin.
top
http://introtopython.org/visualization_earthquakes.html#
18/19
12/28/2015
Otherinterestingdatasetstoexplore
Inthisproject,youlearnedhowtomakeasimplemapandplotpointsonthatmap.Youlearnedhowtopullthosepointsfromalargedataset.Now
thatyouhaveabasicunderstandingofthisprocess,thenextstepistotryyourhandatplottingsomedataofyourown.Youmightstartbyexploring
someofthefollowingdatasets.
GlobalPopulationdata
Thisdatasettiesglobalcensusdatatolatitudeandlongitudegrids.
http://daac.ornl.gov/ISLSCP_II/guides/global_population_xdeg.html(http://daac.ornl.gov/ISLSCP_II/guides/global_population_xdeg.html)
ClimateDatalibrary
Thisincludesover300datafiles,fromavarietyoffieldsrelatedtoearthscienceandclimatology.
http://iridl.ldeo.columbia.edu/(http://iridl.ldeo.columbia.edu/)
USGovRawData
Thisisanotherlargesetofdatasets,aboutavarietyoftopics.ThisiswhereIfoundthedatafortheearthquakevisualizationfeaturedinthetutorial.
https://explore.data.gov/catalog/raw/(https://explore.data.gov/catalog/raw/)
HilaryMasonsResearchQualityDatasets
HilaryMasonisadatascientistatbitly.Ifyouhaveneverheardofher,it'swellworthyourtimetotakealookathersite
(http://www.hilarymason.com/).Ifyouarenewtodatascience,youmightwanttostartwithherepostGettingStartedwithDataScience
(http://www.hilarymason.com/blog/gettingstartedwithdatascience/).Shehasacuratedcollectionofinterestingdatasetshere:
http://bitly.com/bundles/hmason/1(http://bitly.com/bundles/hmason/1)
DEAMethLabdatabase
Thiswouldbealittlehardertoworkwith,becausethelocationsaregivenasaddressesinsteadofbylatitudeandlongitude.Itisalsoreleasedin
pdfformat,whichmightbemorechallengingtoworkwith.Butthisisaprettycompellingtopic,anditwouldbeinterestingtomapoutmethrelated
arrestsandincidentsofviolentcrimeoverdifferentperiodsoftime.
http://www.justice.gov/dea/clanlab/clanlab.shtml(http://www.justice.gov/dea/clanlab/clanlab.shtml)
Other
SmartDisclosureData:
http://www.data.gov/consumer/page/consumerdatapage(http://www.data.gov/consumer/page/consumerdatapage)
100+InterestingDataSetsforStatistics:
http://rs.io/2014/05/29/listofdatasets.html(http://rs.io/2014/05/29/listofdatasets.html)
HNDiscussionwithlinkstomoredatasets(https://news.ycombinator.com/item?id=7818003)
Conclusion
Thatsitforthisproject.Ifyouhaveanothergeospatialdatasettosuggest,orifyoucreatesomethinginteresting,pleasefeelfreetoshareit
(http://github.com/ehmatthes/intro_programming).
top
Home(./)
http://introtopython.org/visualization_earthquakes.html#
19/19