@@ -657,7 +657,7 @@ Earthquakes
657657We'll now use the rain animation to visualize earthquakes on the planet from
658658the last 30 days. The USGS Earthquake Hazards Program is part of the National
659659Earthquake Hazards Reduction Program (NEHRP) and provides several data on their
660- `website <http ://earthquake.usgs.gov >`_. Those data are sorted according to
660+ `website <https ://earthquake.usgs.gov >`_. Those data are sorted according to
661661earthquakes magnitude, ranging from significant only down to all earthquakes,
662662major or minor. You would be surprised by the number of minor earthquakes
663663happening every hour on the planet. Since this would represent too much data
@@ -675,15 +675,14 @@ whose content is given by the first line::
675675
676676We are only interested in latitude, longitude and magnitude and we won't parse
677677time of event (ok, that's bad, feel free to send me a PR).
678-
678+
679679
680680.. code :: python
681681
682682 import urllib
683- from mpl_toolkits.basemap import Basemap
684683
685- # -> http ://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php
686- feed = " http ://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/"
684+ # -> https ://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php
685+ feed = " https ://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/"
687686
688687 # Significant earthquakes in the last 30 days
689688 # url = urllib.request.urlopen(feed + "significant_month.csv")
@@ -701,50 +700,64 @@ time of event (ok, that's bad, feel free to send me a PR).
701700 data = url.read()
702701 data = data.split(b ' \n ' )[+ 1 :- 1 ]
703702 E = np.zeros(len (data), dtype = [(' position' , float , 2 ),
704- (' magnitude' , float , 1 )])
703+ (' magnitude' , float )])
705704
706705 for i in range (len (data)):
707- row = data[i].split(' ,' )
706+ row = data[i].split(b ' ,' )
708707 E[' position' ][i] = float (row[2 ]),float (row[1 ])
709708 E[' magnitude' ][i] = float (row[4 ])
710709
711710
712711 Now, we need to draw the earth on a figure to show precisely where the earthquake
713712center is and to translate latitude/longitude in some coordinates matplotlib
714713can handle. Fortunately, there is the `basemap
715- <http ://matplotlib.org/basemap/> `_ project (that tends to be replaced by the
716- more complete `cartopy <http ://scitools.org.uk/cartopy/ >`_) that is really
714+ <https ://matplotlib.org/basemap/> `_ project (which is now deprecated in favor
715+ of the `cartopy <https ://scitools.org.uk/cartopy/docs/latest/ >`_ project ) that is really
717716simple to install and to use. First step is to define a projection to draw the
718717earth onto a screen (there exists many different projections) and we'll stick
719718to the `mill ` projection which is rather standard for non-specialist like me.
720-
719+
721720
722721.. code :: python
723722
723+ from mpl_toolkits.basemap import Basemap
724724 fig = plt.figure(figsize = (14 ,10 ))
725725 ax = plt.subplot(1 ,1 ,1 )
726726
727- earth = Basemap(projection = ' mill' )
727+ map = Basemap(projection = ' mill' )
728728
729729
730730 Next, we request to draw coastline and fill continents:
731731
732732.. code :: python
733-
734- earth.drawcoastlines(color = ' 0.50' , linewidth = 0.25 )
735- earth.fillcontinents(color = ' 0.95' )
736733
737- The `earth ` object will also be used to translate coordinates quite
738- automatically. We are almost finished. Last step is to adapt the rain code and
739- put some eye candy:
734+ map .drawcoastlines(color = ' 0.50' , linewidth = 0.25 )
735+ map .fillcontinents(color = ' 0.95' )
736+
737+ For cartopy, the steps are quite similar:
738+
739+ .. code :: python
740+
741+ import cartopy
742+ ax = plt.axes(projection = cartopy.crs.Miller())
743+ ax.coastlines(color = ' 0.50' , linewidth = 0.25 )
744+ ax.add_feature(cartopy.feature.LAND , color = ' 0.95' )
745+ ax.set_global()
746+ trans = cartopy.crs.PlateCarree()
747+
748+
749+ We are almost finished. Last step is to adapt the rain code and
750+ put some eye candy. For basemap we use the map object to
751+ transform the coordinates whereas for cartopy we use the transform_point
752+ function of the chosen Miller projection:
740753
741754
742755.. code :: python
743756
744757 P = np.zeros(50 , dtype = [(' position' , float , 2 ),
745- (' size' , float , 1 ),
746- (' growth' , float , 1 ),
747- (' color' , float , 4 )])
758+ (' size' , float ),
759+ (' growth' , float ),
760+ (' color' , float , 4 )])
748761 scat = ax.scatter(P[' position' ][:,0 ], P[' position' ][:,1 ], P[' size' ], lw = 0.5 ,
749762 edgecolors = P[' color' ], facecolors = ' None' , zorder = 10 )
750763
@@ -756,7 +769,8 @@ put some eye candy:
756769 P[' size' ] += P[' growth' ]
757770
758771 magnitude = E[' magnitude' ][current]
759- P[' position' ][i] = earth(* E[' position' ][current])
772+ P[' position' ][i] = map (* E[' position' ][current]) if use_basemap else \
773+ cartopy.crs.Miller().transform_point(* E[' position' ][current], cartopy.crs.PlateCarree())
760774 P[' size' ][i] = 5
761775 P[' growth' ][i]= np.exp(magnitude) * 0.1
762776
@@ -770,8 +784,8 @@ put some eye candy:
770784 scat.set_offsets(P[' position' ])
771785 return scat,
772786
773-
774- animation = FuncAnimation(fig, update, interval = 10 )
787+
788+ animation = FuncAnimation(fig, update, interval = 10 , blit = True )
775789 plt.show()
776790
777791
@@ -781,7 +795,7 @@ If everything went well, you should obtain something like this (with animation):
781795 :target: scripts/earthquakes.py
782796 :width: 50%
783797
784-
798+
785799Other Types of Plots
786800====================
787801
0 commit comments