Overlaying multiple plots on a figure
The City of Seattle has meters installed along the Fremont Bridge that log bicycle traffic on both the East and West side of the bridge, which runs North and South.
In this exercise, you'll use for loops and matplotlib
to explore how the traffic on the East and West sides of the bridge change during the day. Understanding how the two sides of the bridge get used during the morning and evening commute is important for any future development into cycling infrastructure that connects to this high traffic route.
A useful function in Python when writing for loops where you need to keep track of where you are is enumerate()
.
things = ['first thing', 'second', 'yet another']
for ii, item in enumerate(things):
print(ii, item)
0 first thing
1 second
2 yet another
This exercise is part of the course
Python for MATLAB Users
Exercise instructions
- Explore the shape of the array
weekday_traffic
to identify which axes correspond to the side of the bridge and hour of the day, respectively. - Loop over the columns of weekday_traffic, using
enumerate()
to count the number of iterations. - On each loop, plot the column of
weekday_traffic
with the correspondinglabel
insidewalk
. - Create the legend and show the plot.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
sidewalk = ['East','West']
# Explore the shape of the array weekday_traffic
print(weekday_traffic.____)
# Loop over the columns of weekday_traffic, counting the number of iterations
for ii, sidewalk_traffic in ____(weekday_traffic.T):
# Plot the column with the corresponding label in sidewalk
plt.plot(sidewalk_traffic, ____=sidewalk[ii])
# Create the legend and show the plot
plt.____()
____