TDM 20200: Mapping: Project 7 — Spring 2025

Motivation: It is quite easy to plot maps in Python. These skills can be helpful to make visualizations of spatial data.

Context: We will start to learn the basics of drawing maps in Python.

Scope: Mapping

Learning Objectives:
  • We learn how to make maps in Python

Make sure to read about, and use the template found here, and the important information about project submissions here.

Dataset(s)

In this project we will use the following mapping data:

  • /anvil/projects/tdm/data/boundaries/

Questions

Load Pandas as Geopandas as follows:

import pandas as pd
import geopandas as gpd

Question 1 (2 pts)

Read the map data into a variable called mydata as follows:

mydata = gpd.read_file('/anvil/projects/tdm/data/boundaries/cb_2018_us_state_20m.shp')

Display the head of mydata.

What is the shape of mydata (i.e., how many rows and columns are there)?

Plot all of the boundaries in mydata as follows: mydata.plot()

Deliverables
  • Display the head of mydata.

  • Give the shape of mydata (i.e., the number of rows and columns).

  • Plot all of the boundaries in mydata.

  • Be sure to document your work from Question 1, using some comments and insights about your work.

Question 2 (2 pts)

There are 52 rows in mydata. Make a new variable called mycontinentialdata which has all of the rows from mydata except for the rows corresponding to Alaska, Hawaii, and Puerto Rico.

What is the shape of mycontinentialdata (i.e., how many rows and columns are there)?

Plot all of the boundaries in mycontinentialdata. (This should be a map of the 48 continental states, and also the District of Columbia.)

Deliverables
  • Give the shape of mycontinentialdata (i.e., the number of rows and columns).

  • Plot all of the boundaries in mycontinentialdata.

  • Be sure to document your work from Question 2, using some comments and insights about your work.

Question 3 (2 pts)

Now create a new column called mycolors in mycontinentialdata as follows:

mycontinentialdata = mycontinentialdata.assign(mycolors='orange')

and plot the 49 continental states (including DC), now in orange color, as follows:

mycontinentialdata.plot(color = mycontinentialdata['mycolors'])

Now change the color of Indiana to green using:

mycontinentialdata.loc[mycontinentialdata['NAME'] == 'Indiana', 'mycolors'] = 'green'

and plot the 49 continental states (including DC), now in orange color, except with Indiana in a green color, as follows:

mycontinentialdata.plot(color = mycontinentialdata['mycolors'])
Deliverables
  • Plot the 49 continental states (including DC), in orange color.

  • Then also plot the 49 continental states (including DC), in orange color but with Indiana in green color.

  • Be sure to document your work from Question 3, using some comments and insights about your work.

Question 4 (2 pts)

Now change the 5 Midwestern states to green, as follows:

mycontinentialdata.loc[mycontinentialdata['NAME'].isin(['Ohio','Indiana','Illinois','Wisconsin','Michigan']), 'mycolors'] = 'green'

and plot the 49 continental states (including DC), in orange color but with the Midwestern states in green color.

Deliverables
  • Plot the 49 continental states (including DC), in orange color but with the Midwestern states in green color.

  • Be sure to document your work from Question 4, using some comments and insights about your work.

Question 5 (2 pts)

Using the AP News website apnews.com/projects/election-results-2024/?office=P as a guide, make a map of the 49 continental states (including DC), showing whether each state was blue or red in the 2024 Presidential election.

Because Maine and Nebraska split their votes, for the purposes of this map, it is OK to just draw Maine as blue and Nebraska as red.

Deliverables
  • Plot a map of the 49 continental states (including DC), showing whether each state was blue or red in the 2024 Presidential election.

  • Be sure to document your work from Question 5, using some comments and insights about your work.

Submitting your Work

Please make sure that you added comments for each question, which explain your thinking about your method of solving each question. Please also make sure that your work is your own work, and that any outside sources (people, internet pages, generating AI, etc.) are cited properly in the project template.

Congratulations! Assuming you’ve completed all the above questions, you are learning to apply your web scraping knowledge effectively!

Prior to submitting your work, you need to put your work into the project template, and re-run all of the code in your Jupyter notebook and make sure that the results of running that code is visible in your template. Please check the detailed instructions on how to ensure that your submission is formatted correctly. To download your completed project, you can right-click on the file in the file explorer and click 'download'.

Once you upload your submission to Gradescope, make sure that everything appears as you would expect to ensure that you don’t lose any points. We hope your first project with us went well, and we look forward to continuing to learn with you on future projects!!

Items to submit
  • firstname_lastname_project7.ipynb

It is necessary to document your work, with comments about each solution. All of your work needs to be your own work, with citations to any source that you used. Please make sure that your work is your own work, and that any outside sources (people, internet pages, generating AI, etc.) are cited properly in the project template.

You must double check your .ipynb after submitting it in gradescope. A very common mistake is to assume that your .ipynb file has been rendered properly and contains your code, markdown, and code output even though it may not.

Please take the time to double check your work. See here for instructions on how to double check this.

You will not receive full credit if your .ipynb file does not contain all of the information you expect it to, or if it does not render properly in Gradescope. Please ask a TA if you need help with this.