Today's Agenda
4:30 - 4:45PM: Log on & Ice breakers: What is the best costume you’ve ever worn for Halloween?
4:45 - 5:05PM: Definitions
Libraries: A Python library is a reusable chunk of code that you may want to include in your programs/ projects.
append: The append() method appends an element to the end of the list.
with-open: While reading or writing to a file, access mode governs the type of operations possible in the opened file. It refers to how the file will be used once it’s opened.
requests,get(): The get() method sends a GET request to the specified url.
Data Visualization: The process of finding trends and correlations in our data by representing it pictorially is called Data Visualization.
5:05 - 5:10 PM: Bio Break & Guided Python instruction
5:10 - 5:30PM: Guided Python instruction
5:30 - 6:00 PM: Save & Share!
In this course, you will learn the basics of Python programming. We will meet every Tuesday at 4:30pm to 6:00pm. You don't need any previous programming experiences and all you need to code in Python is a web browser (www.replit.com). Mr. Steven will teach you all the core aspects of the Python programming and I will simplify the more complex topics.
Today we will be creating our own adventure story in Python.
Making a scatter plot of US cities by state
Scatter plots are often used to plot 2D data, to look for correlations and other patterns. However, they can also loosely be used to plot geographical X-Y coordinates (in reality, the field of plotting geographical points is far more complicated). We'll use a subset from the city data from simplemaps to generate our next plot. Each row of the data set represents one city in the USA, and gives us its latitude, longitude, and two-letter state code.
Scatter plots are often used to plot 2D data, to look for correlations and other patterns. However, they can also loosely be used to plot geographical X-Y coordinates (in reality, the field of plotting geographical points is far more complicated). We'll use a subset from the city data from simplemaps to generate our next plot. Each row of the data set represents one city in the USA, and gives us its latitude, longitude, and two-letter state code.
from matplotlib import pyplot as plt
import requests
import random
data_url = "https://raw.githubusercontent.com/sixhobbits/ritza/master/data/us-cities.txt"
r = requests.get(data_url)
with open("us-cities.txt", "w") as f:
f.write(r.text)
lats = []
lons = []
colors = []
state_colors = {}
# matplotlib uses single letter shortcuts for common colors
# blue, green, red, cyan, magenta, yellow, black
all_colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
with open("us-cities.txt") as f:
for i, line in enumerate(f):
state, lat, lon = line.split()
lats.append(float(lat))
lons.append(float(lon))
# we assign each state a random color, but once we've picked
# a color we always use it for all cities in that state.
if state not in state_colors:
state_colors[state] = random.choice(all_colors)
colors.append(state_colors[state])
plt.scatter(lons, lats, c=colors)
plt.show()
from matplotlib import pyplot as plt
import requests
import random
data_url = "https://raw.githubusercontent.com/sixhobbits/ritza/master/data/us-cities.txt"
r = requests.get(data_url)
with open("us-cities.txt", "w") as f:
f.write(r.text)
lats = []
lons = []
colors = []
state_colors = {}
# matplotlib uses single letter shortcuts for common colors
# blue, green, red, cyan, magenta, yellow, black
all_colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
with open("us-cities.txt") as f:
for i, line in enumerate(f):
state, lat, lon = line.split()
lats.append(float(lat))
lons.append(float(lon))
# we assign each state a random color, but once we've picked
# a color we always use it for all cities in that state.
if state not in state_colors:
state_colors[state] = random.choice(all_colors)
colors.append(state_colors[state])
plt.scatter(lons, lats, c=colors)
plt.show()
If you run this, you'll notice it takes a little bit longer than the six point plot we created before, as it now has to plot nearly 30 000 data points. Once it's done, you should see something similar to the following (though, as the colours were chosen randomly, yours might look different).