7月sitka天气数据下载
import csv from matplotlib import pyplot as plt from datetime import datetime filename = 'sitka_weather_07-2014.csv' with open(filename) as f: reader = csv.reader(f) header_row = next(reader) # print(header_row) # for index, column_header in enumerate(header_row): # print(index, column_header) # get the highest temperature and plot it in the graph dates = [] highs = [] for row in reader: current_date = datetime.strptime(row[0], "%Y-%m-%d") dates.append(current_date) high = int(row[1]) # if without the "int", the output would all be char highs.append(high) fig = plt.figure(figsize=(10, 6)) plt.plot(dates, highs, c='red') plt.title("Daily high temperatures, July 2014", fontsize=24) plt.xlabel('Date', fontsize=16) plt.ylabel("Temperature (F)", fontsize=16) fig.autofmt_xdate() # so that the date strings do not overlap plt.tick_params(axis='both', which='major', labelsize=16) plt.show()2014年sitka天气数据下载
跟示例1很类似。
import csv from matplotlib import pyplot as plt from datetime import datetime filename = 'sitka_weather_2014.csv' with open(filename) as f: reader = csv.reader(f) header_row = next(reader) # get the highest temperature and plot it in the graph dates = [] highs, lows = [], [] for row in reader: current_date = datetime.strptime(row[0], "%Y-%m-%d") dates.append(current_date) high = int(row[1]) # if without the "int", the output would all be char highs.append(high) low = int(row[3]) lows.append(low) fig = plt.figure(figsize=(10, 6)) plt.plot(dates, highs, c='red', alpha=0.5) # alpha=0 means the color is transparent; 1 is opaque plt.plot(dates, lows, c='blue', alpha=0.5) plt.fill_between(dates, highs, lows, facecolor='green', alpha=0.1) plt.title("Daily high&low temperatures, 2014", fontsize=24) plt.xlabel('Date', fontsize=16) plt.ylabel("Temperature (F)", fontsize=16) fig.autofmt_xdate() # so that the date strings do not overlap plt.tick_params(axis='both', which='major', labelsize=16) plt.show()下载地址
使用的很多数据集都可能缺失数据、数据格式不正确或数据本身不正确。可以尝试用try-except-else 解决; 或者使用remove()或del 将已提取的数据删除
import csv from matplotlib import pyplot as plt from datetime import datetime filename = 'death_valley_2014.csv' with open(filename) as f: reader = csv.reader(f) header_row = next(reader) # get the highest temperature and plot it in the graph dates = [] highs, lows = [], [] for row in reader: try: current_date = datetime.strptime(row[0], "%Y-%m-%d") high = int(row[1]) # if without the "int", the output would all be char low = int(row[3]) except ValueError: print(current_date, 'missing data') else: dates.append(current_date) highs.append(high) lows.append(low) fig = plt.figure(figsize=(10, 6)) plt.plot(dates, highs, c='red', alpha=0.5) # alpha=0 means the color is transparent; 1 is opaque plt.plot(dates, lows, c='blue', alpha=0.5) plt.fill_between(dates, highs, lows, facecolor='green', alpha=0.1) plt.title("Daily high&low temperatures, 2014", fontsize=24) plt.xlabel('Date', fontsize=16) plt.ylabel("Temperature (F)", fontsize=16) fig.autofmt_xdate() # so that the date strings do not overlap plt.tick_params(axis='both', which='major', labelsize=16) plt.show() output--------------------------------------------------------- 2014-02-16 00:00:00 missing data Process finished with exit code 0