Get startedGet started for free

Plotting centroid coordinates

Because we can't plot whole bounding boxes, we summarize the bounding box location into a single point called a centroid. Plotting these on a Basemap map is straightforward. Once we calculate the centroids, we separate the longitudes and latitudes, then pass to the .scatter() method.

The Basemap object m has been created for you. The dataset tweets_sotu and function calculateCentroid() have also been loaded.

This exercise is part of the course

Analyzing Social Media Data in Python

View Course

Exercise instructions

  • Calculate the centroids and store in centroids.
  • Set the argument zorder in fillcontinents such that the continents appear behind the points.
  • Plot the points. Remember to set the latlon argument to the correct value.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Calculate the centroids for the dataset
# and isolate longitudue and latitudes
centroids = ____[____].apply(____)
lon = [x[0] for x in centroids]
lat = [x[1] for x in centroids]

# Draw continents, coastlines, countries, and states
m.fillcontinents(color='white', ____ = ____)
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
m.drawstates(color='gray')

# Draw the points and show the plot
____.____(____, ____, ____ = True, alpha = 0.7)
plt.show()
Edit and Run Code