Python CSV- 繪制氣溫圖表


CSV-  繪制氣溫圖表

資源:

鏈接: https://pan.baidu.com/s/1kqREk-sRnOcC34Mh1lBDHQ 提取碼: uyx7 

 

# highs_lows_Jul.py 最高溫及最低溫
import csv

from matplotlib import pyplot as plt
from datetime import datetime

# 從文件中獲取最高氣溫
filename = 'data/sitka_weather_07-2014.csv'
with open(filename) as f:  # 打開文件
    reader = csv.reader(f)  # 調用csv.reader(),創建一個與文件相關聯的閱讀器對象
    header_row = next(reader)  # 閱讀下一行(標題行)
    
    """
    for index, column_header in enumerate(header_row):  # 獲取索引及值
        print(index, column_header)
    """
    
    """
    dates = [datetime.strptime(row[0], '%Y-%m-%d') for row in reader]  # 使用strptime方法設置日期的格式
    highs = [int((int(row[1]) - 32) / 1.8) for row in reader] # Max TemperatureF列 每行的[1]索引處,並將華氏度轉換為攝氏度
    """
    
    datas = [[datetime.strptime(row[0], '%Y-%m-%d'), int((int(row[1]) - 32) / 1.8)] for row in reader]
    dates = [row[0] for row in datas]  # or [datas[row][0] for row in range(len(datas))]
    highs = [row[1] for row in datas]  # or [datas[row][1] for row in range(len(datas))]

# 根據數據繪制圖形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red')

# 設置圖形的格式
plt.title('Daily high temperatures - July, 2014', fontsize=24)
plt.xlabel('', fontsize=12)
fig.autofmt_xdate()  # 繪制斜的日期標簽,以免重疊
plt.ylabel('Temperature (C)', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=12)

# 添加數值
for x, y in zip(dates, highs):
    plt.text(x, y, y, ha='center', va='bottom', color='blue')

# 修改刻度
plt.xticks(dates[::3])

# 顯示阿拉斯加錫特卡2014年7越每日最高氣溫折線圖
plt.show()

 

# highs_lows_Jul.py 最高溫及最低溫
import csv

from matplotlib import pyplot as plt
from datetime import datetime

# 從文件中獲取最高氣溫
filename = 'data/sitka_weather_2014.csv'
with open(filename) as f:  # 打開文件
    reader = csv.reader(f)  # 調用csv.reader(),創建一個與文件相關聯的閱讀器對象
    header_row = next(reader)  # 閱讀下一行(標題行)

    datas = [[datetime.strptime(row[0], '%Y-%m-%d'), int((int(row[1]) - 32) / 1.8)] for row in reader]
    dates = [row[0] for row in datas]
    highs = [row[1] for row in datas]

# 根據數據繪制圖形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red')

# 設置圖形的格式
plt.title('Daily high temperatures - 2014', fontsize=24)
plt.xlabel('', fontsize=12)
fig.autofmt_xdate()  # 繪制斜的日期標簽,以免重疊
plt.ylabel('Temperature (C)', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=12)

# 修改刻度
plt.xticks(dates[::31])

# 顯示阿拉斯加錫特卡2014年每日最高氣溫折線圖
plt.show()

 

 

# highs_lows_Jul.py 最高溫及最低溫
import csv

from matplotlib import pyplot as plt
from datetime import datetime

# 從文件中獲取最高氣溫
filename = 'data/sitka_weather_2014.csv'
with open(filename) as f:  # 打開文件
    reader = csv.reader(f)  # 調用csv.reader(),創建一個與文件相關聯的閱讀器對象
    header_row = next(reader)  # 閱讀下一行(標題行)

    datas = [[datetime.strptime(row[0], '%Y-%m-%d'), int((int(row[1]) - 32) / 1.8), int((int(row[3]) - 32) / 1.8)] for row in reader]
    dates = [row[0] for row in datas]
    highs = [row[1] for row in datas]
    lows = [row[2] for row in datas]

# 根據數據繪制圖形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha=0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)

# 給圖表區域着色
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.2)  # fill_between(x, y, y, facecolor, alpha)
# alpha值指定顏色的透明度(0為完全透明,1表示完全不透明)


# 設置圖形的格式
plt.title('Daily high and low temperatures - 2014', fontsize=24)
plt.xlabel('', fontsize=12)
fig.autofmt_xdate()  # 繪制斜的日期標簽,以免重疊
plt.ylabel('Temperature (C)', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=12)

# 修改刻度
plt.xticks(dates[::31])

# 顯示阿拉斯加錫特卡2014年每日最高氣溫折線圖
plt.show()

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM