Downloading Data
341
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
# Get dates, and high and low temperatures from this file.
u
dates, highs, lows = [], [], []
for row in reader:
current_date = datetime.strptime(row[2], '%Y-%m-%d')
high = int(row[5])
v
low = int(row[6])
dates.append(current_date)
highs.append(high)
lows.append(low)
# Plot the high and low temperatures.
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.plot(dates, highs, c='red')
w
ax.plot(dates, lows, c='blue')
# Format plot.
x
plt.title("Daily high and low temperatures - 2018", fontsize=24)
--
snip--
At u we
add the empty list
lows
to hold low temperatures, and then
extract and store the low temperature for
each date from the seventh
position in each row (
row[6]
) v. At w we add a call to
plot()
for the low
temperatures and color these values blue. Finally, we update the title x.
Figure 16-4 shows the resulting chart.
Figure 16-4: Two data series on the same plot